UNPKG

quicksilver-cli

Version:
181 lines (180 loc) 7.1 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const parser = __importStar(require("../parser/parser")); const log4js_1 = require("../util/log4js"); const field_1 = require("./field/field"); const HelpUtil = __importStar(require("../util/help")); const all_comands_1 = require("./all-comands"); const ProjectUtil = __importStar(require("../util/project")); const path = __importStar(require("path")); const colors = require("colors"); const log4js = require("log4js"); class Command { constructor(workspace, params) { this.topCommand = path.basename(process.argv[1]); this.cacheKeyWorkspace = "workspace"; this.supportCache = true; this.workspace = workspace; this.params = params; this.rootProjectName = path.basename(this.workspace); this.help = params.filter(item => { return /-{0,2}\?|-{1,2}help/.test(item); }).length > 0; } async init() { } async afterParse() { } async initLogger() { log4js_1.configLogger(this.workspace); this.logger = log4js.getLogger(`${this.topCommand}-cli-${this.name}`); } async putToCache() { ProjectUtil.putCache(this.cacheKeyWorkspace, this.workspace); } async parseParams() { this.args = parser.parseArguments(this.fields, this.params); if (this.args.unrecognizedArguments.length > 0) { this.logger.warn(`Unrecognized Arguments: [${this.args.unrecognizedArguments.join(", ")}].`); } } async validate() { let args = this.args; if (args != null) { for (let field of this.fields) { if (field.default != null && !(field.name in args)) { args[field.name] = field.default; } if (field.enum != null && (field.name in args)) { let value = args[field.name]; if (value != null) { if (Array.isArray(value)) { for (let v of value) { if (field.enum.indexOf(v) == -1) { return Promise.reject(`${field.name} should be in range ${field.enum}.`); } } } else { if (field.enum.indexOf(value) == -1) { return Promise.reject(`${field.name} should be in range ${field.enum}.`); } } } } if (field.hidden) { delete args[field.name]; } if (field.required && !(field.name in args)) { return Promise.reject(`Field '${field.name}' is required.`); } if (field.deprecated && (field.name in args)) { this.logger.warn(`Field '${field.name}' is deprecated.`); } } } } async handle() { try { await this.init(); await this.initLogger(); await this.parseParams(); await this.afterParse(); await this.validate(); if (this.supportCache) { await this.putToCache(); } await this.run(); } catch (e) { if (this.logger == null) { await this.initLogger(); } this.logger.error(e); } } async getHelpMessage() { const messages = []; const fields = this.fields; fields.push({ name: "help", description: "Shows a help message for this command in the console.", type: field_1.FieldType.Boolean }); const argFields = fields.filter(field => "defaultIndex" in field) .sort((a, b) => a.defaultIndex - b.defaultIndex); const optionFields = fields.filter(field => !("defaultIndex" in field)) .sort((a, b) => a.name.localeCompare(b.name)); messages.push("description:"); messages.push(` ${this.description}`); messages.push(""); messages.push("usage:"); let usage = `${this.topCommand} ${this.name}`; if (argFields.length > 0) { const argFieldsUsage = argFields.map(field => `<${field.name}>`).join(" "); usage += ` ${argFieldsUsage}`; } if (optionFields.length > 0) { usage += ` [options]`; } messages.push(` ${usage}`); if (argFields.length > 0) { messages.push(""); messages.push("arguments:"); argFields.forEach(field => { messages.push(` ${colors.cyan(field.name)}`); messages.push(` ${field.description}`); }); } if (optionFields.length > 0) { messages.push(""); messages.push("options:"); optionFields.forEach(field => { let fieldName = colors.cyan("--" + field.name); if (field.aliases) { fieldName += ` (${field.aliases.join(", ")})`; } messages.push(` ${fieldName}`); messages.push(` ${field.description}`); }); } messages.push(""); messages.push("example:"); let example = `${this.topCommand} ${this.name}`; if (argFields.length > 0) { argFields.filter(field => ("example" in field) && !/^\s*$/.test(field.example)) .forEach(field => { example += ` ${field.example}`; }); } if (optionFields.length > 0) { optionFields.filter(field => "example" in field) .forEach(field => { example += ` --${field.name}${/^\s*$/.test(field.example) ? `` : `=${field.example}`}`; }); } messages.push(` ${colors.cyan(example)}`); return messages; } async printHelp() { const messages = await this.getHelpMessage(); HelpUtil.print(...messages); } get name() { return Reflect.get(this.constructor, "NAME"); } get fields() { return Reflect.get(this.constructor, "FIELDS"); } get description() { return Reflect.get(this.constructor, "DESCRIPTION"); } get alias() { return Reflect.get(this.constructor, "ALIAS"); } static getCommandConstructor(commandName) { return all_comands_1.commandMap[commandName]; } } exports.Command = Command;