UNPKG

@anycli/command

Version:
127 lines (126 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // tslint:disable no-implicit-dependencies const pjson = require('../package.json'); const Config = require("@anycli/config"); const Errors = require("@anycli/errors"); const util_1 = require("util"); const flags = require("./flags"); class Command { constructor(argv, config) { this.argv = argv; this.config = config; this.id = this.ctor.id; try { this.debug = require('debug')(this.id ? `${this.config.bin}:${this.id}` : this.config.bin); } catch (_a) { this.debug = () => { }; } } get ctor() { return this.constructor; } async _run() { let err; try { await this.init(); await this.run(); } catch (e) { err = e; await this.catch(e); } finally { await this.finally(err); } } exit(code = 0) { Errors.exit(code); } warn(input) { Errors.warn(input); } error(input, options = {}) { Errors.error(input, options); } log(message = '') { message = typeof message === 'string' ? message : util_1.inspect(message); process.stdout.write(message + '\n'); } async init() { this.debug('init version: %s argv: %o', this.ctor._base, this.argv); if (this.config.debug) Errors.config.debug = true; if (this.config.errlog) Errors.config.errlog = this.config.errlog; // global['cli-ux'].context = global['cli-ux'].context || { // command: compact([this.id, ...this.argv]).join(' '), // version: this.config.userAgent, // } global['http-call'] = global['http-call'] || {}; global['http-call'].userAgent = this.config.userAgent; if (this._helpOverride()) return this._help(); } parse(options, argv = this.argv) { if (!options) options = this.constructor; return require('@anycli/parser').parse(argv, Object.assign({ context: this }, options)); } async catch(err) { if (err.message.match(/Unexpected arguments?: (-h|--help)(,|\n)/)) { this._help(); } else if (err.message.match(/Unexpected arguments?: (-v|--version)(,|\n)/)) { this._version(); } else throw err; } async finally(_) { try { await require('@anycli/errors').config.errorLogger.flush(); // tslint:disable-next-line no-console } catch (err) { console.error(err); } } _help() { const HHelp = require('@anycli/plugin-help').default; const help = new HHelp(this.config); help.showHelp(this.argv); this.exit(0); } _helpOverride() { if (this.argv[0] === '--version' || this.argv[0] === 'version') this._version(); for (let arg of this.argv) { if (arg === '--help') return true; if (arg === '--') return false; } return false; } _version() { this.log(this.config.userAgent); this.exit(0); } } Command._base = `${pjson.name}@${pjson.version}`; Command.aliases = []; Command.strict = true; Command.parse = true; Command.flags = { version: flags.version(), help: flags.help(), }; Command.args = []; Command.parserOptions = {}; /** * instantiate and run the command */ Command.run = async function (argv = process.argv.slice(2), opts) { const config = await Config.load(opts || module.parent && module.parent.parent && module.parent.parent.filename || __dirname); let cmd = new this(argv, config); await cmd._run(argv); }; exports.default = Command;