cli-common: fix workspace check

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-06-04 11:05:12 +02:00
parent daec136f92
commit 142abb0fcf
3 changed files with 34 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli-common': patch
---
The monorepo root check in `findPaths` will now accept a shorthand `workspaces` config in `package.json`, no longer requiring `workspaces.packages`.
+28
View File
@@ -19,6 +19,10 @@ import { resolve as resolvePath } from 'path';
import { findPaths, findRootPath, findOwnDir, findOwnRootDir } from './paths';
describe('paths', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('findOwnDir and findOwnRootDir should find owns paths', () => {
const dir = findOwnDir(__dirname);
const root = findOwnRootDir(dir);
@@ -92,4 +96,28 @@ describe('paths', () => {
resolvePath(root, 'derp.txt'),
);
});
it('findPaths should find workspace root with object', () => {
jest.spyOn(JSON, 'parse').mockReturnValue({ workspaces: { packages: [] } });
jest.spyOn(process, 'cwd').mockReturnValue(__dirname);
const paths = findPaths(__dirname);
expect(paths.targetDir).toBe(
resolvePath(__dirname, '../../cli-common/src'),
);
expect(paths.targetRoot).toBe(resolvePath(__dirname, '../../cli-common'));
});
it('findPaths should find workspace root with array', () => {
jest.spyOn(JSON, 'parse').mockReturnValue({ workspaces: [] });
jest.spyOn(process, 'cwd').mockReturnValue(__dirname);
const paths = findPaths(__dirname);
expect(paths.targetDir).toBe(
resolvePath(__dirname, '../../cli-common/src'),
);
expect(paths.targetRoot).toBe(resolvePath(__dirname, '../../cli-common'));
});
});
+1 -1
View File
@@ -141,7 +141,7 @@ export function findPaths(searchDir: string): Paths {
try {
const content = fs.readFileSync(path, 'utf8');
const data = JSON.parse(content);
return Boolean(data.workspaces?.packages);
return Boolean(data.workspaces);
} catch (error) {
throw new Error(
`Failed to parse package.json file while searching for root, ${error}`,