From 142abb0fcf33f1c16cd7bd946a7d7e7f1c252827 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 4 Jun 2024 11:05:12 +0200 Subject: [PATCH] cli-common: fix workspace check Signed-off-by: Patrik Oldsberg --- .changeset/purple-mugs-beg.md | 5 +++++ packages/cli-common/src/paths.test.ts | 28 +++++++++++++++++++++++++++ packages/cli-common/src/paths.ts | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 .changeset/purple-mugs-beg.md diff --git a/.changeset/purple-mugs-beg.md b/.changeset/purple-mugs-beg.md new file mode 100644 index 0000000000..2507b5d706 --- /dev/null +++ b/.changeset/purple-mugs-beg.md @@ -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`. diff --git a/packages/cli-common/src/paths.test.ts b/packages/cli-common/src/paths.test.ts index 5c1cf8af08..5fcef9cadf 100644 --- a/packages/cli-common/src/paths.test.ts +++ b/packages/cli-common/src/paths.test.ts @@ -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')); + }); }); diff --git a/packages/cli-common/src/paths.ts b/packages/cli-common/src/paths.ts index bdd0245c76..762e660140 100644 --- a/packages/cli-common/src/paths.ts +++ b/packages/cli-common/src/paths.ts @@ -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}`,