From 979cc99c42fc2fd01fb85fcc7b5d9ef9b89e38f5 Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Wed, 24 May 2023 19:23:44 +0300 Subject: [PATCH] fix: handle yarn `wokspaces.packages` directive --- src/main/ts/interface.ts | 2 +- src/main/ts/topo.ts | 8 +++++--- src/test/fixtures/yarn-monorepo/package.json | 8 ++++++++ src/test/fixtures/yarn-monorepo/packages/a/package.json | 4 ++++ src/test/fixtures/yarn-monorepo/packages/b/no-package.txt | 1 + src/test/fixtures/yarn-monorepo/packages/c/package.json | 6 ++++++ .../fixtures/yarn-monorepo/packages/d/d/d/package.json | 3 +++ src/test/fixtures/yarn-monorepo/packages/e/package.json | 3 +++ src/test/ts/topo.ts | 6 ++++++ 9 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/test/fixtures/yarn-monorepo/package.json create mode 100644 src/test/fixtures/yarn-monorepo/packages/a/package.json create mode 100644 src/test/fixtures/yarn-monorepo/packages/b/no-package.txt create mode 100644 src/test/fixtures/yarn-monorepo/packages/c/package.json create mode 100644 src/test/fixtures/yarn-monorepo/packages/d/d/d/package.json create mode 100644 src/test/fixtures/yarn-monorepo/packages/e/package.json diff --git a/src/main/ts/interface.ts b/src/main/ts/interface.ts index 0fe28cf..22c4aef 100644 --- a/src/main/ts/interface.ts +++ b/src/main/ts/interface.ts @@ -4,7 +4,7 @@ export type IPackageDeps = Record export interface IPackageJson { name: string - workspaces?: string[] + workspaces?: string[] | { packages?: string[] } bolt?: { workspaces?: string[] } diff --git a/src/main/ts/topo.ts b/src/main/ts/topo.ts index 282df8b..7c480c8 100644 --- a/src/main/ts/topo.ts +++ b/src/main/ts/topo.ts @@ -101,7 +101,7 @@ export const topo = async ( pkgFilter, workspacesExtra, workspaces: [ - ...(workspaces || (await getWorkspaces(root))), + ...(workspaces || (await extractWorkspaces(root))), ...workspacesExtra ] } @@ -128,8 +128,10 @@ export const topo = async ( } } -export const getWorkspaces = async (root: IPackageEntry) => - root.manifest.workspaces || +export const extractWorkspaces = async (root: IPackageEntry) => + (Array.isArray(root.manifest.workspaces) + ? root.manifest.workspaces + : root.manifest.workspaces?.packages) || root.manifest.bolt?.workspaces || (await (async () => { try { diff --git a/src/test/fixtures/yarn-monorepo/package.json b/src/test/fixtures/yarn-monorepo/package.json new file mode 100644 index 0000000..65cbd36 --- /dev/null +++ b/src/test/fixtures/yarn-monorepo/package.json @@ -0,0 +1,8 @@ +{ + "name": "root", + "workspaces": { + "packages": [ + "packages/*" + ] + } +} diff --git a/src/test/fixtures/yarn-monorepo/packages/a/package.json b/src/test/fixtures/yarn-monorepo/packages/a/package.json new file mode 100644 index 0000000..72403eb --- /dev/null +++ b/src/test/fixtures/yarn-monorepo/packages/a/package.json @@ -0,0 +1,4 @@ +{ + "name": "a", + "private": true +} diff --git a/src/test/fixtures/yarn-monorepo/packages/b/no-package.txt b/src/test/fixtures/yarn-monorepo/packages/b/no-package.txt new file mode 100644 index 0000000..c17aaf7 --- /dev/null +++ b/src/test/fixtures/yarn-monorepo/packages/b/no-package.txt @@ -0,0 +1 @@ +no package.json diff --git a/src/test/fixtures/yarn-monorepo/packages/c/package.json b/src/test/fixtures/yarn-monorepo/packages/c/package.json new file mode 100644 index 0000000..d74f2ec --- /dev/null +++ b/src/test/fixtures/yarn-monorepo/packages/c/package.json @@ -0,0 +1,6 @@ +{ + "name": "c", + "dependencies": { + "e": "*" + } +} diff --git a/src/test/fixtures/yarn-monorepo/packages/d/d/d/package.json b/src/test/fixtures/yarn-monorepo/packages/d/d/d/package.json new file mode 100644 index 0000000..d045002 --- /dev/null +++ b/src/test/fixtures/yarn-monorepo/packages/d/d/d/package.json @@ -0,0 +1,3 @@ +{ + "name": "d" +} diff --git a/src/test/fixtures/yarn-monorepo/packages/e/package.json b/src/test/fixtures/yarn-monorepo/packages/e/package.json new file mode 100644 index 0000000..0c8a6a0 --- /dev/null +++ b/src/test/fixtures/yarn-monorepo/packages/e/package.json @@ -0,0 +1,3 @@ +{ + "name": "e" +} diff --git a/src/test/ts/topo.ts b/src/test/ts/topo.ts index ebc1fae..dc3dc0b 100644 --- a/src/test/ts/topo.ts +++ b/src/test/ts/topo.ts @@ -292,6 +292,12 @@ test('`topo` processes bolt monorepos', async () => { assert.equal(result.nodes, ['a', 'c', 'e']) }) +test('`topo` processes yarn monorepos with `workspaces.packages` directive', async () => { + const cwd = resolve(fixtures, 'yarn-monorepo') + const result = await topo({ cwd }) + assert.equal(result.nodes, ['a', 'c', 'e']) +}) + test('`topo` injects packages to workspaces via workspacesExtra', async () => { const cwd = resolve(fixtures, 'regular-monorepo') const workspaces = ['packages/*']