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); }); 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( 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); }); 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(