Skip to content

Commit

Permalink
feat(run-lifecycle): Remove figgy-pudding
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Nov 24, 2020
1 parent 3b0e2fe commit 1093f87
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions utils/run-lifecycle/__tests__/run-lifecycle.test.js
Expand Up @@ -86,14 +86,14 @@ describe("runLifecycle()", () => {
};
const dir = pkg.location;
const stage = "dashed-options";
const opts = new Map([
["ignore-prepublish", true],
["ignore-scripts", false],
["node-options", "--a-thing"],
["script-shell", "fish"],
["scripts-prepend-node-path", true],
["unsafe-perm", false],
]);
const opts = {
"ignore-prepublish": true,
"ignore-scripts": false,
"node-options": "--a-thing",
"script-shell": "fish",
"scripts-prepend-node-path": true,
"unsafe-perm": false,
};

await runLifecycle(pkg, stage, opts);

Expand All @@ -120,7 +120,9 @@ describe("runLifecycle()", () => {
},
};
const stage = "prepublish";
const opts = new Map().set("ignore-prepublish", true);
const opts = {
"ignore-prepublish": true,
};

await runLifecycle(pkg, stage, opts);

Expand All @@ -135,7 +137,9 @@ describe("runLifecycle()", () => {
},
};
const stage = "ignored";
const opts = new Map().set("ignore-scripts", true);
const opts = {
"ignore-scripts": true,
};

await runLifecycle(pkg, stage, opts);

Expand All @@ -152,7 +156,7 @@ describe("runLifecycle()", () => {
},
};
const stage = "prepack";
const opts = new Map();
const opts = {};

await runLifecycle(pkg, stage, opts);

Expand Down
1 change: 0 additions & 1 deletion utils/run-lifecycle/package.json
Expand Up @@ -32,7 +32,6 @@
},
"dependencies": {
"@lerna/npm-conf": "file:../npm-conf",
"figgy-pudding": "^3.5.1",
"npm-lifecycle": "^3.1.2",
"npmlog": "^4.1.2"
}
Expand Down
75 changes: 42 additions & 33 deletions utils/run-lifecycle/run-lifecycle.js
Expand Up @@ -2,49 +2,58 @@

const log = require("npmlog");
const runScript = require("npm-lifecycle");
const figgyPudding = require("figgy-pudding");
const npmConf = require("@lerna/npm-conf");

module.exports = runLifecycle;
module.exports.createRunner = createRunner;

const LifecycleConfig = figgyPudding(
{
log: { default: log },
// provide aliases for some dash-cased props
"ignore-prepublish": {},
ignorePrepublish: "ignore-prepublish",
"ignore-scripts": {},
ignoreScripts: "ignore-scripts",
"node-options": {},
nodeOptions: "node-options",
"script-shell": {},
scriptShell: "script-shell",
"scripts-prepend-node-path": {},
scriptsPrependNodePath: "scripts-prepend-node-path",
"unsafe-perm": {
// when running scripts explicitly, assume that they're trusted
default: true,
},
unsafePerm: "unsafe-perm",
},
{
other() {
// open up the pudding
return true;
},
}
);
/**
* @typedef {object} LifecycleConfig
* @property {typeof log} [log]
* @property {boolean} [ignorePrepublish]
* @property {boolean} [ignoreScripts]
* @property {string} [nodeOptions]
* @property {string} [scriptShell]
* @property {boolean} [scriptsPrependNodePath]
* @property {boolean} [unsafePerm=true]
*/

/**
* Alias dash-cased npmConf to camelCase
* @param {LifecycleConfig} obj
* @returns {LifecycleConfig}
*/
function flattenOptions(obj) {
return {
ignorePrepublish: obj["ignore-prepublish"],
ignoreScripts: obj["ignore-scripts"],
nodeOptions: obj["node-options"],
scriptShell: obj["script-shell"],
scriptsPrependNodePath: obj["scripts-prepend-node-path"],
unsafePerm: obj["unsafe-perm"],
...obj,
};
}

function runLifecycle(pkg, stage, _opts) {
/**
* Run a lifecycle script for a package.
* @param {import("@lerna/package")} pkg
* @param {string} stage
* @param {LifecycleConfig} options
*/
function runLifecycle(pkg, stage, options) {
// back-compat for @lerna/npm-conf instances
// https://github.com/isaacs/proto-list/blob/27764cd/proto-list.js#L14
if ("root" in _opts) {
if ("root" in options) {
// eslint-disable-next-line no-param-reassign
_opts = _opts.snapshot;
options = options.snapshot;
}

const opts = LifecycleConfig(_opts);
const opts = {
log,
unsafePerm: true,
...flattenOptions(options),
};
const dir = pkg.location;
const config = {};

Expand All @@ -67,7 +76,7 @@ function runLifecycle(pkg, stage, _opts) {
}

// https://github.com/zkat/figgy-pudding/blob/7d68bd3/index.js#L42-L64
for (const [key, val] of opts) {
for (const [key, val] of Object.entries(opts)) {
// omit falsy values and circular objects
if (val != null && key !== "log" && key !== "logstream") {
config[key] = val;
Expand Down

0 comments on commit 1093f87

Please sign in to comment.