UNPKG

@zambelz/zhc

Version:
73 lines 2.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createCommand = void 0; const chalk_1 = __importDefault(require("chalk")); const common_1 = require("./common"); const parseOptions = (args) => { const result = {}; for (let i = 0; i < args.length; i++) { let key = ""; if (args[i].startsWith("-")) { key = args[i].slice(1); } if (args[i].startsWith("--")) { key = args[i].slice(2); } const valueIndex = i + 1; const value = args[valueIndex]; const isKeyEmpty = key.trim() === ""; if (!isKeyEmpty && value && (!value.startsWith("-") || !value.startsWith("--"))) { result[key] = value; } if (!isKeyEmpty && !value) { result[key] = true; } } return result; }; const createCommand = (cmd, action) => { return { ...cmd, action: (args) => { const option = parseOptions(args); const hasHelpOpt = Object.keys(option) .filter((opt) => opt === "h" || opt === "help").length > 0; if (cmd.command !== "base" && hasHelpOpt) { const space = (0, common_1.spacer)(); console.log(); console.log(chalk_1.default.green("ZHC CLI")); console.log(); console.log(cmd.description); console.log(); console.log(`${chalk_1.default.yellow("Usage:")} zhc ${cmd.command} [options]`); console.log(); console.log(`${chalk_1.default.yellow("Options:")}`); for (const [key, value] of Object.entries(cmd.options)) { const pairedKey = key.split(":"); const shortOpt = chalk_1.default.green(`-${pairedKey[0]}`); const longOpt = chalk_1.default.green(`--${pairedKey[1]}`); console.log(`${space}${shortOpt}, ${longOpt}\t\t${value}`); } return; } const optKeys = Object.keys(cmd.options); const splittedOptKeys = optKeys.map((opt) => opt.split(":")); for (const key in option) { for (const splittedOptKey of splittedOptKeys) { if (splittedOptKey.includes(key)) { option[splittedOptKey[1]] = option[key]; if (key.length === 1) { delete option[key]; } } } } action(option); } }; }; exports.createCommand = createCommand; //# sourceMappingURL=cmd.js.map