UNPKG

@eljs/create

Version:
96 lines (94 loc) 4.28 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_update_notifier = __toESM(require("update-notifier")); var import_core = require("./core"); var import_utils2 = require("./utils"); var debug = (0, import_utils.createDebugger)("create:cli"); cli().then(() => process.exit(0)).catch((error) => { if (error instanceof import_utils2.AppError) { import_utils.logger.error(error.message); } else { console.error(error); } 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("create").description("Create a project from a remote template").version(pkg.version, "-v, --version", "Output the current version").arguments("<template> <project-name>").option("--cwd <cwd>", "Specify the working directory").option("-f, --force", "Overwrite target directory if it exists").option("-m, --merge", "Merge target directory if it exists").option("--no-install", "Skip install dependencies after create done").action(async (template, projectName, options) => { debug == null ? void 0 : debug(`template:`, template); debug == null ? void 0 : debug(`projectName:`, projectName); debug == null ? void 0 : debug(`options:%O`, options); await new import_core.Create({ ...options, template }).run(projectName); }); 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); }; }