Skip to content
Permalink
Browse files

feat(project): Add `getPackagesSync()` export

Scratching an itch.
  • Loading branch information
evocateur committed May 13, 2020
1 parent 1713635 commit 068bdd75683ca9687a420523e4ba007bd8b3b220
@@ -0,0 +1,4 @@
{
"name": "pkg-1",
"version": "1.0.0"
}
@@ -0,0 +1,7 @@
{
"name": "pkg-2",
"version": "1.0.0",
"dependencies": {
"pkg-1": "^1.0.0"
}
}
@@ -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);
@@ -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) {
@@ -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 });
}
@@ -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();
@@ -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 = {
@@ -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.
You can’t perform that action at this time.