UNPKG

@eljs/release

Version:
137 lines (135 loc) 5.99 kB
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 )); // src/cli.ts var import_utils = require("@eljs/utils"); var import_commander = require("commander"); var import_node_path = __toESM(require("node:path")); var import_semver = __toESM(require("semver")); var import_update_notifier = __toESM(require("update-notifier")); var import_release = require("./release"); var import_utils2 = require("./utils"); var debug = (0, import_utils.createDebugger)("release:cli"); cli().then(() => process.exit(0)).catch(() => { process.exit(1); }); process.on("SIGINT", () => { (0, import_utils2.onCancel)(); }); async function cli() { const pkg = await (0, import_utils.readJson)( import_node_path.default.join(__dirname, "../package.json") ); (0, import_update_notifier.default)({ pkg }).notify(); import_commander.program.name("release").description("Release npm package easily").version(pkg.version, "-v, --version", "Output the current version").argument("[version]", "Specify the bump version", checkVersion).option("--cwd <cwd>", "Specify the working directory").option("--git.independent", "Generate git tag independent").option("--no-git.requireClean", "Skip git working tree clean check").option("--no-git.changelog", "Skip changelog generation").option("--no-git.commit", "Skip git commit").option("--no-git.push", "Skip git push").option( "--git.requireBranch <requireBranch>", "Require that the release is on a particular branch" ).option("--npm.prerelease", "Specify the release type as prerelease").option("--npm.canary", "Specify the release type as canary").option("--no-npm.requireOwner", "Skip npm owner check").option("--no-npm.confirm", "Skip confirm bump version").option("--npm.prereleaseId <prereleaseId>", "Specify the prereleaseId").option("--no-github.release", "Skip github release").action(async (version, opts) => { debug == null ? void 0 : debug(`version:`, version); debug == null ? void 0 : debug(`opts:%O`, opts); const options = parseOptions(opts); debug == null ? void 0 : debug(`options:%O`, options); await (0, import_release.release)(version, options); }); enhanceErrorMessages("missingArgument", (argName) => { return `Missing required argument ${import_utils.chalk.yellow(`<${argName}>`)}.`; }); enhanceErrorMessages("unknownOption", (optionName) => { return `Unknown option ${import_utils.chalk.yellow(optionName)}.`; }); enhanceErrorMessages("optionMissingArgument", (option, flag) => { return `Missing required argument for option ${import_utils.chalk.yellow(option.flags)}` + (flag ? `, got ${import_utils.chalk.yellow(flag)}` : ``); }); enhanceExcessArguments(); await import_commander.program.parseAsync(process.argv); } function enhanceErrorMessages(methodName, log) { ; import_commander.Command["prototype"][methodName] = function(...args) { if (methodName === "unknownOption" && this._allowUnknownOption) { return; } this.outputHelp(); console.log(); console.log(` ` + import_utils.chalk.red(log(...args))); console.log(); process.exit(1); }; } function enhanceExcessArguments() { ; import_commander.Command["prototype"]["_excessArguments"] = function(receivedArgs) { if (this._allowExcessArguments) return; const expected = this.registeredArguments.length; const s = expected === 1 ? "" : "s"; const message = `Expected ${expected} argument${s} but got ${import_utils.chalk.yellow(receivedArgs.length)}.`; this.outputHelp(); console.log(); console.log(` ` + import_utils.chalk.red(message)); console.log(); process.exit(1); }; } function checkVersion(value) { if (import_semver.RELEASE_TYPES.includes(value)) { return value; } if (!import_semver.default.valid(value)) { throw new import_commander.InvalidArgumentError(`Invalid semantic version \`${value}\`.`); } if (value.indexOf("v") === 0) { return value.substring(1); } return value; } function parseOptions(options) { const result = {}; for (const [path2, value] of Object.entries(options)) { if (!path2.includes(".")) { result[path2] = value; continue; } const keys = path2.split(".").filter(Boolean); let current = result; for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (i === keys.length - 1) { current[key] = value; } else { current[key] = current[key] || {}; current = current[key]; } } } const { git, npm } = result; for (const key of Object.keys(git)) { if (["requireClean", "changelog", "commit", "push"].includes(key) && git[key] === true) { Reflect.deleteProperty(git, key); } } for (const key of Object.keys(npm)) { if (["requireOwner", "confirm"].includes(key) && npm[key] === true) { Reflect.deleteProperty(npm, key); } } return result; }