@lerna/publish
Version:
Publish packages in the current project
162 lines (161 loc) • 6.29 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_fs_extra = __toESM(require("fs-extra"));
var import_p_map = __toESM(require("p-map"));
var import_path = __toESM(require("path"));
const childProcess = require("@lerna/child-process");
module.exports = function factory(argv) {
return new InitCommand(argv);
};
class InitCommand extends import_core.Command {
exact;
lernaVersion;
get requiresGit() {
return false;
}
runValidations() {
this.logger.verbose(this.name, "skipping validations");
}
// TODO: refactor based on TS feedback
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
runPreparations() {
this.logger.verbose(this.name, "skipping preparations");
}
initialize() {
this.exact = this.options.exact;
this.lernaVersion = this.options.lernaVersion;
if (!this.gitInitialized()) {
this.logger.info("", "Initializing Git repository");
return childProcess.exec("git", ["init"], this.execOpts);
}
}
execute() {
let chain = Promise.resolve();
chain = chain.then(() => this.ensureGitIgnore());
chain = chain.then(() => this.ensureConfig());
chain = chain.then(() => this.ensurePackagesDir());
return chain.then(() => {
this.logger.success("", "Initialized Lerna files");
this.logger.info("", "New to Lerna? Check out the docs: https://lerna.js.org/docs/getting-started");
});
}
ensureGitIgnore() {
const gitIgnorePath = import_path.default.join(this.project.rootPath, ".gitignore");
let chain = Promise.resolve();
if (!import_fs_extra.default.existsSync(gitIgnorePath)) {
this.logger.info("", "Creating .gitignore");
chain = chain.then(() => import_fs_extra.default.writeFile(gitIgnorePath, "node_modules/"));
}
return chain;
}
ensureConfig() {
const hasExistingLernaConfig = !!this.project.version;
const hasExistingPackageJson = !!this.project.manifest;
const useNx = !hasExistingLernaConfig || this.project.config.useNx !== false;
const useWorkspaces = !hasExistingLernaConfig || this.project.config.useWorkspaces === true;
let chain = Promise.resolve();
if (!hasExistingPackageJson) {
this.logger.info("", "Creating package.json");
chain = chain.then(() => {
const pkg = {
name: "root",
private: true
};
if (useWorkspaces) {
pkg.workspaces = [import_core.Project.PACKAGE_GLOB];
}
return (0, import_devkit.writeJsonFile)(import_path.default.join(this.project.rootPath, "package.json"), pkg, {
spaces: 2
});
});
} else {
this.logger.info("", "Updating package.json");
chain = chain.then(() => {
if (useWorkspaces && !this.project.manifest.get("workspaces")) {
this.project.manifest.set("workspaces", [import_core.Project.PACKAGE_GLOB]);
return this.project.manifest.serialize();
}
});
}
chain = chain.then(() => {
const rootPkg = this.project.manifest;
const setDependency = ({ name, version }) => {
let targetDependencies;
if (rootPkg.dependencies && rootPkg.dependencies[name]) {
targetDependencies = rootPkg.dependencies;
} else {
if (!rootPkg.devDependencies) {
rootPkg.set("devDependencies", {});
}
targetDependencies = rootPkg.devDependencies;
}
targetDependencies[name] = this.exact ? version : `^${version}`;
};
setDependency({ name: "lerna", version: this.lernaVersion });
return rootPkg.serialize();
});
chain = chain.then(() => {
let version;
if (this.options.independent) {
version = "independent";
} else if (this.project.version) {
version = this.project.version;
} else {
version = "0.0.0";
}
if (!hasExistingLernaConfig) {
this.logger.info("", "Creating lerna.json");
} else {
this.logger.info("", "Updating lerna.json");
if (!useWorkspaces && !this.project.config.packages) {
Object.assign(this.project.config, {
packages: [import_core.Project.PACKAGE_GLOB]
});
}
}
delete this.project.config.lerna;
if (this.exact) {
const commandConfig = this.project.config.command || (this.project.config.command = {});
const initConfig = commandConfig.init || (commandConfig.init = {});
initConfig.exact = true;
}
Object.assign(this.project.config, {
$schema: "node_modules/lerna/schemas/lerna-schema.json",
useWorkspaces,
version,
// Only set if explicitly disabling
useNx: useNx === false ? false : void 0
});
return Promise.resolve(this.project.serializeConfig());
});
return chain;
}
ensurePackagesDir() {
this.logger.info("", "Creating packages directory");
return (0, import_p_map.default)(this.project.packageParentDirs, (dir) => import_fs_extra.default.mkdirp(dir));
}
}
module.exports.InitCommand = InitCommand;