Skip to content

Commit

Permalink
feat(project): Add getPackagesSync() export
Browse files Browse the repository at this point in the history
Scratching an itch.
  • Loading branch information
evocateur committed May 13, 2020
1 parent 1713635 commit 068bdd7
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/project/__fixtures__/basic/packages/pkg-1/package.json
@@ -0,0 +1,4 @@
{
"name": "pkg-1",
"version": "1.0.0"
}
7 changes: 7 additions & 0 deletions core/project/__fixtures__/basic/packages/pkg-2/package.json
@@ -0,0 +1,7 @@
{
"name": "pkg-2",
"version": "1.0.0",
"dependencies": {
"pkg-1": "^1.0.0"
}
}
25 changes: 25 additions & 0 deletions core/project/__tests__/project.test.js
Expand Up @@ -258,6 +258,31 @@ Object {
});
});

describe(".getPackagesSync()", () => {
it("returns a list of package instances synchronously", () => {
const project = new Project(testDir);
expect(project.getPackagesSync()).toMatchInlineSnapshot(`
Array [
Object {
"name": "pkg-1",
"version": "1.0.0",
},
Object {
"dependencies": Object {
"pkg-1": "^1.0.0",
},
"name": "pkg-2",
"version": "1.0.0",
},
]
`);
});

it("is available from a static named export", () => {
expect(Project.getPackagesSync(testDir)).toMatchObject(new Project(testDir).getPackagesSync());
});
});

describe("get .manifest", () => {
it("returns a Package instance", () => {
const project = new Project(testDir);
Expand Down
13 changes: 12 additions & 1 deletion core/project/index.js
Expand Up @@ -14,7 +14,7 @@ const ValidationError = require("@lerna/validation-error");
const Package = require("@lerna/package");
const applyExtends = require("./lib/apply-extends");
const deprecateConfig = require("./lib/deprecate-config");
const { makeFileFinder } = require("./lib/make-file-finder");
const { makeFileFinder, makeSyncFileFinder } = require("./lib/make-file-finder");

class Project {
constructor(cwd) {
Expand Down Expand Up @@ -175,6 +175,16 @@ class Project {
return this.fileFinder("package.json", filePaths => pMap(filePaths, mapper, { concurrency: 50 }));
}

getPackagesSync() {
return makeSyncFileFinder(this.rootPath, this.packageConfigs)("package.json", packageConfigPath => {
return new Package(
loadJsonFile.sync(packageConfigPath),
path.dirname(packageConfigPath),
this.rootPath
);
});
}

getPackageLicensePaths() {
return this.fileFinder(Project.LICENSE_GLOB, null, { case: false });
}
Expand All @@ -196,3 +206,4 @@ Project.LICENSE_GLOB = "LICEN{S,C}E{,.*}";

module.exports = Project;
module.exports.getPackages = cwd => new Project(cwd).getPackages();
module.exports.getPackagesSync = cwd => new Project(cwd).getPackagesSync();
19 changes: 19 additions & 0 deletions core/project/lib/make-file-finder.js
Expand Up @@ -6,6 +6,7 @@ const path = require("path");
const ValidationError = require("@lerna/validation-error");

module.exports.makeFileFinder = makeFileFinder;
module.exports.makeSyncFileFinder = makeSyncFileFinder;

function getGlobOpts(rootPath, packageConfigs) {
const globOpts = {
Expand Down Expand Up @@ -61,3 +62,21 @@ function makeFileFinder(rootPath, packageConfigs) {
return promise.then(results => results.reduce((acc, result) => acc.concat(result), []));
};
}

function makeSyncFileFinder(rootPath, packageConfigs) {
const globOpts = getGlobOpts(rootPath, packageConfigs);

return (fileName, fileMapper, customGlobOpts) => {
const options = Object.assign({}, customGlobOpts, globOpts);
const patterns = packageConfigs.map(globPath => path.join(globPath, fileName)).sort();

let results = globby.sync(patterns, options);

/* istanbul ignore else */
if (fileMapper) {
results = results.map(fileMapper);
}

return results;
};
}

0 comments on commit 068bdd7

Please sign in to comment.