From d67ddc741507086cbf3b507b357c035dbaa4eade Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 13 Aug 2018 13:31:39 +0900 Subject: [PATCH 1/4] :white_check_mark: Add failure test case of top-level package.json --- .../monorepo-lerna/__tests__/index.spec.js | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/@hothouse/monorepo-lerna/__tests__/index.spec.js b/packages/@hothouse/monorepo-lerna/__tests__/index.spec.js index 17a538b..134bd7b 100644 --- a/packages/@hothouse/monorepo-lerna/__tests__/index.spec.js +++ b/packages/@hothouse/monorepo-lerna/__tests__/index.spec.js @@ -17,6 +17,18 @@ test("Lerna#match should returns true if directory not have lerna.json", async ( ); }); +test("Lerna#getPackages should include top-level package.json", async () => { + const lerna = new Lerna(); + const pkg = path.join(__dirname, "fixtures", "lockfile-npm"); + const expected = [ + path.join(pkg), + path.join(pkg, "packages", "has-not-package-lock-json"), + path.join(pkg, "packages", "has-package-lock-json") + ]; + const actual = await lerna.getPackages(pkg); + expect(expected).toEqual(actual); +}); + test("Lerna#getChanges should return [prefix/package.json] when lockfile not exists", async () => { const lerna = new Lerna(); const prefix = path.join("packages", "has-not-package-lock-json"); @@ -32,7 +44,7 @@ test("Lerna#getChanges should return [prefix/package.json] when lockfile not exi path.join(__dirname, "fixtures", "lockfile-npm"), new Npm() ); - assert.deepStrictEqual(actual, expected); + expect(expected).toEqual(actual); }); test("Lerna#getChanges should return [prefix/package.json, prefix/package-lock.json] when npmClient=npm lockfile exists", async () => { const lerna = new Lerna(); @@ -52,7 +64,7 @@ test("Lerna#getChanges should return [prefix/package.json, prefix/package-lock.j path.join(__dirname, "fixtures", "lockfile-npm"), new Npm() ); - assert.deepStrictEqual(actual, expected); + expect(expected).toEqual(actual); }); test("Lerna#getChanges should return [prefix/package.json] when npmClient=yarn lockfile not exists", async () => { const lerna = new Lerna(); @@ -69,7 +81,7 @@ test("Lerna#getChanges should return [prefix/package.json] when npmClient=yarn l path.join(__dirname, "fixtures", "lockfile-yarn"), new Yarn() ); - assert.deepStrictEqual(actual, expected); + expect(expected).toEqual(actual); }); test("Lerna#getChanges should return [prefix/package.json, prefix/yarn.lock] when npmClient=yarn lockfile exists", async () => { const lerna = new Lerna(); @@ -89,5 +101,5 @@ test("Lerna#getChanges should return [prefix/package.json, prefix/yarn.lock] whe path.join(__dirname, "fixtures", "lockfile-yarn"), new Yarn() ); - assert.deepStrictEqual(actual, expected); + expect(expected).toEqual(actual); }); From 29d2961e8b50bb6098361d824cb3a7f3dd961be2 Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 13 Aug 2018 13:34:59 +0900 Subject: [PATCH 2/4] :bug: Lerna#getPackages includes top-level package --- .../@hothouse/monorepo-lerna/src/index.js | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/@hothouse/monorepo-lerna/src/index.js b/packages/@hothouse/monorepo-lerna/src/index.js index 1dea0a1..08b7d82 100644 --- a/packages/@hothouse/monorepo-lerna/src/index.js +++ b/packages/@hothouse/monorepo-lerna/src/index.js @@ -15,20 +15,22 @@ class Lerna implements Structure { async getPackages(directory: string): Promise> { // $FlowFixMe(dynamic-require) const settings = require(path.join(directory, "lerna.json")); - return settings.packages - .reduce((acc, pkg) => acc.concat(glob.sync(pkg, { absolute: true })), []) - .filter(packagePath => { - const isPackage = fs.existsSync(path.join(packagePath, "package.json")); - if (!isPackage) { - debug( - `${path.relative( - directory, - packagePath - )} is not a npm package. Ignored` - ); - } - return isPackage; - }); + const children = settings.packages.reduce((acc, pattern) => { + const prefix = path.join(directory, pattern); + return acc.concat(glob.sync(prefix, { absolute: true })); + }, []); + return [directory, ...children].filter(packagePath => { + const isPackage = fs.existsSync(path.join(packagePath, "package.json")); + if (!isPackage) { + debug( + `${path.relative( + directory, + packagePath + )} is not a npm package. Ignored` + ); + } + return isPackage; + }); } async install( From 6e42a22632c7af492e3f581e408eb699327e0ca3 Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 13 Aug 2018 13:38:00 +0900 Subject: [PATCH 3/4] :white_check_mark: Add failure test case of YarnWorkspaces#getPackages --- .../__tests__/index.spec.js | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/@hothouse/monorepo-yarn-workspaces/__tests__/index.spec.js b/packages/@hothouse/monorepo-yarn-workspaces/__tests__/index.spec.js index e22c5d0..6359e93 100644 --- a/packages/@hothouse/monorepo-yarn-workspaces/__tests__/index.spec.js +++ b/packages/@hothouse/monorepo-yarn-workspaces/__tests__/index.spec.js @@ -29,34 +29,37 @@ test("YarnWorkspaces#match must returns false if directory not have package.json ); }); -test("YarnWorkspaces#getPackages should return [package.json, yarn.lock] when lockfile exists", async () => { - const lerna = new YarnWorkspaces(); +test("YarnWorkspaces#getPackages should return package when lockfile exists", async () => { + const yarnWorkspaces = new YarnWorkspaces(); const prefix = path.join( __dirname, "fixtures", "has-yarn-workspaces-with-lockfile" ); - const expected = [path.join(prefix, "packages", "child-a")]; - const actual = await lerna.getPackages(prefix); - assert.deepStrictEqual(actual, expected); + const expected = [ + path.join(prefix), + path.join(prefix, "packages", "child-a") + ]; + const actual = await yarnWorkspaces.getPackages(prefix); + expect(expected).toEqual(actual); }); test("YarnWorkspaces#getChanges should return [package.json] when lockfile not exists", async () => { - const lerna = new YarnWorkspaces(); + const yarnWorkspaces = new YarnWorkspaces(); const expected = new Set(["package.json"]); - const actual = await lerna.getChanges( + const actual = await yarnWorkspaces.getChanges( path.join(__dirname, "fixtures", "is-yarn-workspaces"), path.join(__dirname, "fixtures", "is-yarn-workspaces") ); - assert.deepStrictEqual(actual, expected); + expect(expected).toEqual(actual); }); test("YarnWorkspaces#getChanges should return [package.json, yarn.lock] when lockfile exists", async () => { - const lerna = new YarnWorkspaces(); + const yarnWorkspaces = new YarnWorkspaces(); const expected = new Set([ path.join("packages", "child-a", "package.json"), "yarn.lock" ]); - const actual = await lerna.getChanges( + const actual = await yarnWorkspaces.getChanges( path.join( __dirname, "fixtures", @@ -66,5 +69,5 @@ test("YarnWorkspaces#getChanges should return [package.json, yarn.lock] when loc ), path.join(__dirname, "fixtures", "has-yarn-workspaces-with-lockfile") ); - assert.deepStrictEqual(actual, expected); + expect(expected).toEqual(actual); }); From 02b4450672625b62ae648fa6cc5d07f744c08420 Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 13 Aug 2018 13:47:46 +0900 Subject: [PATCH 4/4] :bug: YarnWorkspaces#getPackages now includes top-level package --- .../monorepo-yarn-workspaces/src/index.js | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/@hothouse/monorepo-yarn-workspaces/src/index.js b/packages/@hothouse/monorepo-yarn-workspaces/src/index.js index b151022..24b1afd 100644 --- a/packages/@hothouse/monorepo-yarn-workspaces/src/index.js +++ b/packages/@hothouse/monorepo-yarn-workspaces/src/index.js @@ -19,27 +19,25 @@ class YarnWorkspaces implements Structure { async getPackages(directory: string): Promise> { // $FlowFixMe(dynamic-require) const settings = require(path.join(directory, "package.json")); - const packages = Array.isArray(settings.workspaces) + const globs = Array.isArray(settings.workspaces) ? settings.workspaces : settings.workspaces.packages; - return packages - .reduce( - (acc, pkg) => - acc.concat(glob.sync(path.join(directory, pkg), { absolute: true })), - [] - ) - .filter(packagePath => { - const isPackage = fs.existsSync(path.join(packagePath, "package.json")); - if (!isPackage) { - debug( - `${path.relative( - directory, - packagePath - )} is not a npm package. Ignored` - ); - } - return isPackage; - }); + const children = globs.reduce((acc, pattern) => { + const prefix = path.join(directory, pattern); + return acc.concat(glob.sync(prefix, { absolute: true })); + }, []); + return [directory, ...children].filter(packagePath => { + const isPackage = fs.existsSync(path.join(packagePath, "package.json")); + if (!isPackage) { + debug( + `${path.relative( + directory, + packagePath + )} is not a npm package. Ignored` + ); + } + return isPackage; + }); } async install(