@lerna/publish
Version:
Publish packages in the current project
134 lines (133 loc) • 4.7 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var run_lifecycle_exports = {};
__export(run_lifecycle_exports, {
createRunner: () => createRunner,
runLifecycle: () => runLifecycle
});
module.exports = __toCommonJS(run_lifecycle_exports);
var import_npmlog = __toESM(require("npmlog"));
var import_p_queue = __toESM(require("p-queue"));
const runScript = require("@npmcli/run-script");
const npmConf = require("./npm-conf");
const queue = new import_p_queue.default({ concurrency: 1 });
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 printCommandBanner(id, event, cmd, path) {
return console.log(`
> ${id ? `${id} ` : ""}${event} ${path}
> ${cmd.trim().replace(/\n/g, "\n> ")}
`);
}
function runLifecycle(pkg, stage, options) {
if ("root" in options) {
options = options.snapshot;
}
const opts = {
log: import_npmlog.default,
unsafePerm: true,
...flattenOptions(options)
};
const dir = pkg.location;
const id = `${pkg.name}@${pkg.version}`;
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();
}
for (const [key, val] of Object.entries(opts)) {
if (val != null && key !== "log" && key !== "logstream") {
config[key] = val;
}
}
if (pkg.__isLernaPackage) {
pkg = pkg.toJSON();
}
pkg._id = id;
opts.log.silly("lifecycle", "%j starting in %j", stage, pkg.name);
opts.log.info("lifecycle", `${id}~${stage}: ${id}`);
const stdio = opts.stdio || "pipe";
if (import_npmlog.default.level !== "silent") {
printCommandBanner(id, stage, pkg.scripts[stage], dir);
}
return queue.add(
async () => runScript({
event: stage,
path: dir,
pkg,
args: [],
stdio,
banner: false,
// TODO: refactor based on TS feedback
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
scriptShell: config.scriptShell
}).then(
({ stdout }) => {
if (stdout) {
process.stdout.write(stdout.toString().trimEnd() + "\n");
}
opts.log.silly("lifecycle", "%j finished in %j", stage, pkg.name);
},
(err) => {
const exitCode = err.code || 1;
import_npmlog.default.error("lifecycle", "%j errored in %j, exiting %d", stage, pkg.name, exitCode);
err.name = "ValidationError";
err.exitCode = exitCode;
process.exitCode = exitCode;
throw err;
}
)
);
}
function createRunner(commandOptions) {
const cfg = npmConf(commandOptions).snapshot;
return (pkg, stage) => runLifecycle(pkg, stage, cfg);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createRunner,
runLifecycle
});