Skip to content

Commit

Permalink
fix(run-lifecycle): Do not execute on packages that lack the target s…
Browse files Browse the repository at this point in the history
…cript, avoiding spurious logs
  • Loading branch information
evocateur committed Dec 20, 2018
1 parent dde588a commit c0ad316
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions commands/__mocks__/@lerna/run-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const mockRunLifecycle = jest.fn(pkg => Promise.resolve(pkg));
const mockCreateRunner = jest.fn(() => (pkg, stage) => {
// no longer the actual API, but approximates inner logic of default export
if (pkg.scripts[stage]) {
return mockRunLifecycle(pkg, stage);
}
Expand Down
34 changes: 34 additions & 0 deletions utils/run-lifecycle/__tests__/run-lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,39 @@ const npmConf = require("@lerna/npm-conf");
const runLifecycle = require("../run-lifecycle");

describe("runLifecycle()", () => {
it("skips packages without scripts", async () => {
const pkg = {
name: "no-scripts",
};

const result = await runLifecycle(pkg, "foo", new Map());

expect(result).toBe(pkg);
expect(runScript).not.toHaveBeenCalled();
});

it("skips packages without matching script", async () => {
const pkg = {
name: "missing-script",
scripts: {
test: "foo",
},
};

const result = await runLifecycle(pkg, "bar", new Map());

expect(result).toBe(pkg);
expect(runScript).not.toHaveBeenCalled();
});

it("calls npm-lifecycle with prepared arguments", async () => {
const pkg = {
name: "test-name",
version: "1.0.0-test",
location: "test-location",
scripts: {
preversion: "test",
},
};
const stage = "preversion";
const opts = npmConf({ "custom-cli-flag": true });
Expand Down Expand Up @@ -47,6 +75,9 @@ describe("runLifecycle()", () => {
name: "dashed-name",
version: "1.0.0-dashed",
location: "dashed-location",
scripts: {
prepublish: "test",
},
};
const dir = pkg.location;
const stage = "prepublish";
Expand Down Expand Up @@ -82,6 +113,9 @@ describe("runLifecycle()", () => {
name: "circular-name",
version: "1.0.0-circular",
location: "circular-location",
scripts: {
prepack: "test",
},
};
const stage = "prepack";
const opts = new Map();
Expand Down
14 changes: 7 additions & 7 deletions utils/run-lifecycle/run-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ function runLifecycle(pkg, stage, _opts) {
const dir = pkg.location;
const config = {};

if (!pkg.scripts || !pkg.scripts[stage]) {
opts.log.silly("run-lifecycle", "No script for %j in %j, continuing", stage, pkg.name);

return Promise.resolve(pkg);
}

// https://github.com/zkat/figgy-pudding/blob/7d68bd3/index.js#L42-L64
for (const [key, val] of opts) {
// omit falsy values and circular objects
Expand Down Expand Up @@ -107,11 +113,5 @@ function runLifecycle(pkg, stage, _opts) {
function createRunner(commandOptions) {
const cfg = npmConf(commandOptions).snapshot;

return (pkg, stage) => {
if (pkg.scripts && pkg.scripts[stage]) {
return runLifecycle(pkg, stage, cfg);
}

return Promise.resolve(pkg);
};
return (pkg, stage) => runLifecycle(pkg, stage, cfg);
}

0 comments on commit c0ad316

Please sign in to comment.