From 8dd3ca0eeccdd5ef7de9740c14d3be645bc609b9 Mon Sep 17 00:00:00 2001 From: Ran Yitzhaki Date: Wed, 8 Jan 2020 17:23:44 +0200 Subject: [PATCH] add yarn workspace support --- src/__tests__/__snapshots__/cli.spec.ts.snap | 11 +++++ src/__tests__/cli.spec.ts | 7 +++ .../monorepo-with-workspaces/lerna.json | 4 ++ .../node_modules/package-bar/package.json | 4 ++ .../node_modules/package-baz/package.json | 4 ++ .../node_modules/package-foo/package.json | 4 ++ .../monorepo-with-workspaces/package.json | 7 +++ .../node_modules/package-foo/package.json | 4 ++ .../packages/package-a/package.json | 4 ++ .../node_modules/package-bar/package.json | 4 ++ .../packages/package-b/package.json | 4 ++ src/workspace/workspace.ts | 43 ++++++++++++++----- 12 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/lerna.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-bar/package.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-baz/package.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-foo/package.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/package.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/node_modules/package-foo/package.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/package.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/node_modules/package-bar/package.json create mode 100644 src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/package.json diff --git a/src/__tests__/__snapshots__/cli.spec.ts.snap b/src/__tests__/__snapshots__/cli.spec.ts.snap index 795fa66..de1eacb 100644 --- a/src/__tests__/__snapshots__/cli.spec.ts.snap +++ b/src/__tests__/__snapshots__/cli.spec.ts.snap @@ -46,6 +46,17 @@ package-a " `; +exports[`CLI qnm ] should work in monorepo with yarn workspaces 1`] = ` +"package-foo +└── 1.0.0 + +package-a +└─┬ package-foo + └── 2.0.0 + +" +`; + exports[`CLI qnm list should --disable-colors 1`] = ` "anotherDependency └── 1.0.0 diff --git a/src/__tests__/cli.spec.ts b/src/__tests__/cli.spec.ts index fe1e4a9..a01d6bc 100644 --- a/src/__tests__/cli.spec.ts +++ b/src/__tests__/cli.spec.ts @@ -60,6 +60,13 @@ describe('CLI', () => { expect(output).toMatchSnapshot(); }); + + it.only('should work in monorepo with yarn workspaces', () => { + const cwd = resolveFixture('monorepo-with-workspaces'); + const output = runCommand('package-foo', { cwd }); + + expect(output).toMatchSnapshot(); + }); }); describe('qnm list', () => { diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/lerna.json b/src/__tests__/fixtures/monorepo-with-workspaces/lerna.json new file mode 100644 index 0000000..f1fe31c --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/lerna.json @@ -0,0 +1,4 @@ +{ + "lerna": "3.15.0", + "useWorkspaces": true +} \ No newline at end of file diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-bar/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-bar/package.json new file mode 100644 index 0000000..24717c8 --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-bar/package.json @@ -0,0 +1,4 @@ +{ + "name": "package-bar", + "version": "1.0.0" +} diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-baz/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-baz/package.json new file mode 100644 index 0000000..8af780d --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-baz/package.json @@ -0,0 +1,4 @@ +{ + "name": "package-baz", + "version": "1.0.0" +} diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-foo/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-foo/package.json new file mode 100644 index 0000000..b93f052 --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/node_modules/package-foo/package.json @@ -0,0 +1,4 @@ +{ + "name": "package-foo", + "version": "1.0.0" +} diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/package.json new file mode 100644 index 0000000..c76206f --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/package.json @@ -0,0 +1,7 @@ +{ + "name": "monorepo-with-workspaces", + "private": true, + "workspaces": [ + "packages/*" + ] +} diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/node_modules/package-foo/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/node_modules/package-foo/package.json new file mode 100644 index 0000000..5c2b16d --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/node_modules/package-foo/package.json @@ -0,0 +1,4 @@ +{ + "name": "package-foo", + "version": "2.0.0" +} diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/package.json new file mode 100644 index 0000000..bb84166 --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-a/package.json @@ -0,0 +1,4 @@ +{ + "name": "package-a", + "version": "1.0.0" +} diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/node_modules/package-bar/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/node_modules/package-bar/package.json new file mode 100644 index 0000000..88f7f6b --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/node_modules/package-bar/package.json @@ -0,0 +1,4 @@ +{ + "name": "package-bar", + "version": "3.0.0" +} diff --git a/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/package.json b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/package.json new file mode 100644 index 0000000..6f51818 --- /dev/null +++ b/src/__tests__/fixtures/monorepo-with-workspaces/packages/package-b/package.json @@ -0,0 +1,4 @@ +{ + "name": "package-b", + "version": "1.0.0" +} diff --git a/src/workspace/workspace.ts b/src/workspace/workspace.ts index bbf4125..7bc71c6 100644 --- a/src/workspace/workspace.ts +++ b/src/workspace/workspace.ts @@ -8,6 +8,11 @@ import { isTruthy } from '../utils'; import ModulesMap from './modules-map'; import NodeModule from './node-module'; +type Workspaces = + | Array + | { packages: Array | undefined } + | undefined; + type YarnLock = Record< string, { version: string; dependencies: Record } @@ -191,17 +196,33 @@ export default class Workspace { const lernaJsonPath = path.join(this.root, 'lerna.json'); const lernaJson = JSON.parse(fs.readFileSync(lernaJsonPath, 'utf8')); - // TODO - add yarn workspaces support - const packages = globby.sync(lernaJson.packages, { - absolute: true, - onlyDirectories: true, - }); - - packages.forEach(location => { - try { - this.packages.push(Workspace.loadSync(location, false)); - } catch (error) {} - }); + let packages = lernaJson.packages; + + if (lernaJson.useWorkspaces === true) { + const workspaces = this.packageJson.workspaces as Workspaces; + + if (!Array.isArray(workspaces)) { + packages = workspaces?.packages; + } else { + packages = workspaces; + } + } + + if (!packages) { + console.warn('No packages found for monorepo'); + console.warn(`packages data wasn't loaded`); + } + + globby + .sync(packages, { + absolute: true, + onlyDirectories: true, + }) + .forEach(location => { + try { + this.packages.push(Workspace.loadSync(location, false)); + } catch (error) {} + }); } static loadSync(cwd = process.cwd(), traverse = true): Workspace {