@lerna/publish
Version:
Publish packages in the current project
141 lines (140 loc) • 5.52 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 npm_install_exports = {};
__export(npm_install_exports, {
npmInstall: () => npmInstall,
npmInstallDependencies: () => npmInstallDependencies
});
module.exports = __toCommonJS(npm_install_exports);
var import_fs_extra = __toESM(require("fs-extra"));
var import_npm_package_arg = __toESM(require("npm-package-arg"));
var import_npmlog = __toESM(require("npmlog"));
var import_signal_exit = __toESM(require("signal-exit"));
var import_write_pkg = __toESM(require("write-pkg"));
var import_get_npm_exec_opts = require("./get-npm-exec-opts");
const childProcess = require("@lerna/child-process");
module.exports.npmInstallDependencies = npmInstallDependencies;
function npmInstall(pkg, { registry, npmClient, npmClientArgs, npmGlobalStyle, mutex, stdio = "pipe", subCommand = "install" }) {
const opts = (0, import_get_npm_exec_opts.getNpmExecOpts)(pkg, registry);
const args = [subCommand];
let cmd = npmClient || "npm";
if (npmGlobalStyle) {
cmd = "npm";
args.push("--global-style");
}
if (cmd === "yarn" && mutex) {
args.push("--mutex", mutex);
}
if (cmd === "yarn") {
args.push("--non-interactive");
}
if (npmClientArgs && npmClientArgs.length) {
args.push(...npmClientArgs);
}
opts.stdio = stdio;
opts.env.LERNA_EXEC_PATH = pkg.location;
opts.env.LERNA_ROOT_PATH = pkg.rootPath;
import_npmlog.default.silly("npmInstall", [cmd, args]);
return childProcess.exec(cmd, args, opts);
}
function npmInstallDependencies(pkg, dependencies, config) {
import_npmlog.default.silly("npmInstallDependencies", pkg.name, dependencies);
if (!(dependencies && dependencies.length)) {
import_npmlog.default.verbose("npmInstallDependencies", "no dependencies to install");
return Promise.resolve();
}
const packageJsonBkp = `${pkg.manifestLocation}.lerna_backup`;
import_npmlog.default.silly("npmInstallDependencies", "backup", pkg.manifestLocation);
return import_fs_extra.default.copy(pkg.manifestLocation, packageJsonBkp).then(() => {
const cleanup = () => {
import_npmlog.default.silly("npmInstallDependencies", "cleanup", pkg.manifestLocation);
import_fs_extra.default.renameSync(packageJsonBkp, pkg.manifestLocation);
};
const unregister = (0, import_signal_exit.default)(cleanup);
const done = (finalError) => {
cleanup();
unregister();
if (finalError) {
throw finalError;
}
};
const tempJson = transformManifest(pkg, dependencies);
import_npmlog.default.silly("npmInstallDependencies", "writing tempJson", tempJson);
return (0, import_write_pkg.default)(pkg.manifestLocation, tempJson).then(() => npmInstall(pkg, config)).then(() => done(), done);
});
}
function transformManifest(pkg, dependencies) {
const json = pkg.toJSON();
const depMap = new Map(
dependencies.map((dep) => {
const { name, rawSpec } = (0, import_npm_package_arg.default)(dep, pkg.location);
return [name, rawSpec || "*"];
})
);
delete json.scripts;
["dependencies", "devDependencies", "optionalDependencies"].forEach((depType) => {
const collection = json[depType];
if (collection) {
Object.keys(collection).forEach((depName) => {
if (depMap.has(depName)) {
collection[depName] = depMap.get(depName);
depMap.delete(depName);
} else {
delete collection[depName];
}
});
}
});
["bundledDependencies", "bundleDependencies"].forEach((depType) => {
const collection = json[depType];
if (Array.isArray(collection)) {
const newCollection = [];
for (const depName of collection) {
if (depMap.has(depName)) {
newCollection.push(depName);
depMap.delete(depName);
}
}
json[depType] = newCollection;
}
});
if (depMap.size) {
if (!json.dependencies) {
json.dependencies = {};
}
depMap.forEach((depVersion, depName) => {
json.dependencies[depName] = depVersion;
});
}
return json;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
npmInstall,
npmInstallDependencies
});