Skip to content

Commit

Permalink
fix(run-lifecycle): Short-circuit ignore options
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Jan 2, 2019
1 parent 0ef71fb commit ae29097
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
38 changes: 33 additions & 5 deletions utils/run-lifecycle/__tests__/run-lifecycle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ describe("runLifecycle()", () => {
version: "1.0.0-dashed",
location: "dashed-location",
scripts: {
prepublish: "test",
"dashed-options": "test",
},
};
const dir = pkg.location;
const stage = "prepublish";
const stage = "dashed-options";
const opts = new Map([
["ignore-prepublish", true],
["ignore-scripts", true],
["ignore-scripts", false],
["node-options", true],
["script-shell", true],
["scripts-prepend-node-path", true],
Expand All @@ -96,15 +96,43 @@ describe("runLifecycle()", () => {
dir,
failOk: false,
log: expect.any(Object),
ignorePrepublish: true,
ignoreScripts: true,
nodeOptions: true,
scriptShell: true,
scriptsPrependNodePath: true,
unsafePerm: true,
});
});

it("ignores prepublish when configured", async () => {
const pkg = {
name: "ignore-prepublish",
scripts: {
prepublish: "test",
},
};
const stage = "prepublish";
const opts = new Map().set("ignore-prepublish", true);

await runLifecycle(pkg, stage, opts);

expect(runScript).not.toHaveBeenCalled();
});

it("ignores scripts when configured", async () => {
const pkg = {
name: "ignore-scripts",
scripts: {
ignored: "test",
},
};
const stage = "ignored";
const opts = new Map().set("ignore-scripts", true);

await runLifecycle(pkg, stage, opts);

expect(runScript).not.toHaveBeenCalled();
});

it("omits circular opts", async () => {
const pkg = {
name: "circular-name",
Expand Down
33 changes: 17 additions & 16 deletions utils/run-lifecycle/run-lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,24 @@ function runLifecycle(pkg, stage, _opts) {
const dir = pkg.location;
const config = {};

if (opts.ignoreScripts) {
opts.log.verbose("lifecycle", "%j ignored in %j", stage, pkg.name);

return Promise.resolve();
}

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

return Promise.resolve();
}

if (stage === "prepublish" && opts.ignorePrepublish) {
opts.log.verbose("lifecycle", "%j ignored in %j", stage, pkg.name);

return Promise.resolve();
}

// 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 All @@ -65,29 +77,18 @@ function runLifecycle(pkg, stage, _opts) {
// TODO: remove pkg._id when npm-lifecycle no longer relies on it
pkg._id = `${pkg.name}@${pkg.version}`; // eslint-disable-line

// bring along camelCased aliases
const {
ignorePrepublish,
ignoreScripts,
nodeOptions,
scriptShell,
scriptsPrependNodePath,
unsafePerm,
} = opts;

opts.log.silly("lifecycle", "%j starting in %j", stage, pkg.name);

return runScript(pkg, stage, dir, {
config,
dir,
failOk: false,
log: opts.log,
ignorePrepublish,
ignoreScripts,
nodeOptions,
scriptShell,
scriptsPrependNodePath,
unsafePerm,
// bring along camelCased aliases
nodeOptions: opts.nodeOptions,
scriptShell: opts.scriptShell,
scriptsPrependNodePath: opts.scriptsPrependNodePath,
unsafePerm: opts.unsafePerm,
}).then(
() => {
opts.log.silly("lifecycle", "%j finished in %j", stage, pkg.name);
Expand Down

0 comments on commit ae29097

Please sign in to comment.