@lerna/publish
Version:
Publish packages in the current project
148 lines (147 loc) • 5.47 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 __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 import_core = require("@lerna/core");
var import_p_map = __toESM(require("p-map"));
const childProcess = require("@lerna/child-process");
module.exports = function factory(argv) {
return new ExecCommand(argv);
};
class ExecCommand extends import_core.Command {
get requiresGit() {
return false;
}
initialize() {
const dashedArgs = this.options["--"] || [];
this.command = this.options.cmd || dashedArgs.shift();
this.args = (this.options.args || []).concat(dashedArgs);
if (!this.command) {
throw new import_core.ValidationError("ENOCOMMAND", "A command to execute is required");
}
this.bail = this.options.bail !== false;
this.prefix = this.options.prefix !== false;
this.env = Object.assign({}, process.env);
let chain = Promise.resolve();
chain = chain.then(() => (0, import_core.getFilteredPackages)(this.packageGraph, this.execOpts, this.options));
chain = chain.then((filteredPackages) => {
this.filteredPackages = filteredPackages;
});
return chain.then(() => {
this.count = this.filteredPackages.length;
this.packagePlural = this.count === 1 ? "package" : "packages";
this.joinedCommand = [this.command].concat(this.args).join(" ");
});
}
execute() {
this.logger.info(
"",
"Executing command in %d %s: %j",
this.count,
this.packagePlural,
this.joinedCommand
);
let chain = Promise.resolve();
if (this.options.parallel) {
chain = chain.then(() => this.runCommandInPackagesParallel());
} else if (this.toposort) {
chain = chain.then(() => this.runCommandInPackagesTopological());
} else {
chain = chain.then(() => this.runCommandInPackagesLexical());
}
if (this.bail) {
chain = chain.catch((err) => {
process.exitCode = err.exitCode;
throw err;
});
} else {
chain = chain.then((results) => {
if (results.some((result) => result.failed)) {
const codes = results.filter((result) => result.failed).map((result) => result.exitCode);
const exitCode = Math.max(...codes, 1);
this.logger.error("", "Received non-zero exit code %d during execution", exitCode);
process.exitCode = exitCode;
}
});
}
return chain.then(() => {
this.logger.success(
"exec",
"Executed command in %d %s: %j",
this.count,
this.packagePlural,
this.joinedCommand
);
});
}
getOpts(pkg) {
return {
cwd: pkg.location,
shell: true,
extendEnv: false,
env: Object.assign({}, this.env, {
LERNA_PACKAGE_NAME: pkg.name,
LERNA_ROOT_PATH: this.project.rootPath
}),
reject: this.bail,
pkg
};
}
getRunner() {
return this.options.stream ? (pkg) => this.runCommandInPackageStreaming(pkg) : (pkg) => this.runCommandInPackageCapturing(pkg);
}
runCommandInPackagesTopological() {
let profiler;
let runner;
if (this.options.profile) {
profiler = new import_core.Profiler({
concurrency: this.concurrency,
log: this.logger,
outputDirectory: this.options.profileLocation || this.project.rootPath
});
const callback = this.getRunner();
runner = (pkg) => profiler.run(() => callback(pkg), pkg.name);
} else {
runner = this.getRunner();
}
let chain = (0, import_core.runTopologically)(this.filteredPackages, runner, {
concurrency: this.concurrency,
rejectCycles: this.options.rejectCycles
});
if (profiler) {
chain = chain.then((results) => profiler.output().then(() => results));
}
return chain;
}
runCommandInPackagesParallel() {
return (0, import_p_map.default)(this.filteredPackages, (pkg) => this.runCommandInPackageStreaming(pkg));
}
runCommandInPackagesLexical() {
return (0, import_p_map.default)(this.filteredPackages, this.getRunner(), { concurrency: this.concurrency });
}
runCommandInPackageStreaming(pkg) {
return childProcess.spawnStreaming(this.command, this.args, this.getOpts(pkg), this.prefix && pkg.name);
}
runCommandInPackageCapturing(pkg) {
return childProcess.spawn(this.command, this.args, this.getOpts(pkg));
}
}
module.exports.ExecCommand = ExecCommand;