@lerna/publish
Version:
Publish packages in the current project
139 lines (138 loc) • 6.19 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_devkit = require("@nrwl/devkit");
var import_inquirer = __toESM(require("inquirer"));
var import_npmlog = __toESM(require("npmlog"));
module.exports = function factory(argv) {
return new AddCachingCommand(argv);
};
class AddCachingCommand extends import_core.Command {
uniqueScriptNames = [];
constructor(argv) {
super(argv, { skipValidations: true });
}
initialize() {
if (this.options.useNx === false) {
this.logger.error(
"add-caching",
"The `add-caching` command is only available when using the Nx task runner (do not set `useNx` to `false` in `lerna.json`)"
);
process.exit(1);
}
const packages = this.packageGraph?.rawPackageList || [];
const uniqueScriptNames = /* @__PURE__ */ new Set();
for (const pkg of packages) {
for (const scriptName of Object.keys(pkg.scripts || {})) {
uniqueScriptNames.add(scriptName);
}
}
this.uniqueScriptNames = Array.from(uniqueScriptNames);
}
async execute() {
this.logger.info(
"add-caching",
"Please answer the following questions about the scripts found in your workspace in order to generate task runner configuration"
);
process.stdout.write("\n");
import_npmlog.default.pause();
const { targetDefaults } = await import_inquirer.default.prompt([
{
type: "checkbox",
name: "targetDefaults",
message: "Which scripts need to be run in order? (e.g. before building a project, dependent projects must be built.)\n",
choices: this.uniqueScriptNames
}
]);
const { cacheableOperations } = await import_inquirer.default.prompt([
{
type: "checkbox",
name: "cacheableOperations",
message: "Which scripts are cacheable? (Produce the same output given the same input, e.g. build, test and lint usually are, serve and start are not.)\n",
choices: this.uniqueScriptNames
}
]);
const scriptOutputs = {};
for (const scriptName of cacheableOperations) {
scriptOutputs[scriptName] = await import_inquirer.default.prompt([
{
type: "input",
name: scriptName,
message: `Does the "${scriptName}" script create any outputs? If not, leave blank, otherwise provide a path relative to a project root (e.g. dist, lib, build, coverage)
`
}
]);
}
import_npmlog.default.resume();
process.stdout.write("\n");
this.convertAnswersToNxConfig({ cacheableOperations, targetDefaults, scriptOutputs });
this.logger["success"]("add-caching", "Successfully updated task runner configuration in `nx.json`");
this.logger.info(
"add-caching",
"Learn more about task runner configuration here: https://lerna.js.org/docs/concepts/task-pipeline-configuration"
);
this.logger.info(
"add-caching",
"Note that the legacy task runner options of --sort, --no-sort and --parallel no longer apply. Learn more here: https://lerna.js.org/docs/lerna6-obsolete-options"
);
}
convertAnswersToNxConfig(answers) {
const nxJsonPath = (0, import_devkit.joinPathFragments)(import_devkit.workspaceRoot, "nx.json");
let nxJson = {};
try {
nxJson = (0, import_devkit.readJsonFile)(nxJsonPath);
} catch {
}
nxJson.tasksRunnerOptions = nxJson.tasksRunnerOptions || {};
nxJson.tasksRunnerOptions["default"] = nxJson.tasksRunnerOptions["default"] || {};
nxJson.tasksRunnerOptions["default"].runner = nxJson.tasksRunnerOptions["default"].runner || "nx/tasks-runners/default";
nxJson.tasksRunnerOptions["default"].options = nxJson.tasksRunnerOptions["default"].options || {};
if (nxJson.tasksRunnerOptions["default"].options.cacheableOperations) {
this.logger.warn(
"add-caching",
"The `tasksRunnerOptions.default.cacheableOperations` property already exists in `nx.json` and will be overwritten by your answers"
);
}
nxJson.tasksRunnerOptions["default"].options.cacheableOperations = answers.cacheableOperations;
if (nxJson.targetDefaults) {
this.logger.warn(
"add-caching",
"The `targetDefaults` property already exists in `nx.json` and will be overwritten by your answers"
);
}
nxJson.targetDefaults = nxJson.targetDefaults || {};
for (const scriptName of answers.targetDefaults) {
nxJson.targetDefaults[scriptName] = nxJson.targetDefaults[scriptName] || {};
nxJson.targetDefaults[scriptName] = { dependsOn: [`^${scriptName}`] };
}
for (const [scriptName, scriptAnswerData] of Object.entries(answers.scriptOutputs)) {
if (!scriptAnswerData[scriptName]) {
continue;
}
nxJson.targetDefaults[scriptName] = nxJson.targetDefaults[scriptName] || {};
nxJson.targetDefaults[scriptName].outputs = [`{projectRoot}/${scriptAnswerData[scriptName]}`];
}
(0, import_devkit.writeJsonFile)(nxJsonPath, nxJson);
}
}
module.exports.AddCachingCommand = AddCachingCommand;