| 1 | /* eslint-disable class-methods-use-this */
|
|---|
| 2 | 'use strict';
|
|---|
| 3 |
|
|---|
| 4 | const
|
|---|
| 5 | UTIL = require('util'),
|
|---|
| 6 | PATH = require('path'),
|
|---|
| 7 | EOL = require('os').EOL,
|
|---|
| 8 |
|
|---|
| 9 | Q = require('q'),
|
|---|
| 10 | chalk = require('chalk'),
|
|---|
| 11 |
|
|---|
| 12 | CoaObject = require('./coaobject'),
|
|---|
| 13 | Opt = require('./opt'),
|
|---|
| 14 | Arg = require('./arg'),
|
|---|
| 15 | completion = require('./completion');
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Command
|
|---|
| 19 | *
|
|---|
| 20 | * Top level entity. Commands may have options and arguments.
|
|---|
| 21 | *
|
|---|
| 22 | * @namespace
|
|---|
| 23 | * @class Cmd
|
|---|
| 24 | * @extends CoaObject
|
|---|
| 25 | */
|
|---|
| 26 | class Cmd extends CoaObject {
|
|---|
| 27 | /**
|
|---|
| 28 | * @constructs
|
|---|
| 29 | * @param {COA.Cmd} [cmd] parent command
|
|---|
| 30 | */
|
|---|
| 31 | constructor(cmd) {
|
|---|
| 32 | super(cmd);
|
|---|
| 33 |
|
|---|
| 34 | this._parent(cmd);
|
|---|
| 35 | this._cmds = [];
|
|---|
| 36 | this._cmdsByName = {};
|
|---|
| 37 | this._opts = [];
|
|---|
| 38 | this._optsByKey = {};
|
|---|
| 39 | this._args = [];
|
|---|
| 40 | this._api = null;
|
|---|
| 41 | this._ext = false;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | static create(cmd) {
|
|---|
| 45 | return new Cmd(cmd);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Returns object containing all its subcommands as methods
|
|---|
| 50 | * to use from other programs.
|
|---|
| 51 | *
|
|---|
| 52 | * @returns {Object}
|
|---|
| 53 | */
|
|---|
| 54 | get api() {
|
|---|
| 55 | // Need _this here because of passed arguments into _api
|
|---|
| 56 | const _this = this;
|
|---|
| 57 | this._api || (this._api = function () {
|
|---|
| 58 | return _this.invoke.apply(_this, arguments);
|
|---|
| 59 | });
|
|---|
| 60 |
|
|---|
| 61 | const cmds = this._cmdsByName;
|
|---|
| 62 | Object.keys(cmds).forEach(cmd => { this._api[cmd] = cmds[cmd].api; });
|
|---|
| 63 |
|
|---|
| 64 | return this._api;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | _parent(cmd) {
|
|---|
| 68 | this._cmd = cmd || this;
|
|---|
| 69 |
|
|---|
| 70 | this.isRootCmd ||
|
|---|
| 71 | cmd._cmds.push(this) &&
|
|---|
| 72 | this._name &&
|
|---|
| 73 | (this._cmd._cmdsByName[this._name] = this);
|
|---|
| 74 |
|
|---|
| 75 | return this;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | get isRootCmd() {
|
|---|
| 79 | return this._cmd === this;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Set a canonical command identifier to be used anywhere in the API.
|
|---|
| 84 | *
|
|---|
| 85 | * @param {String} name - command name
|
|---|
| 86 | * @returns {COA.Cmd} - this instance (for chainability)
|
|---|
| 87 | */
|
|---|
| 88 | name(name) {
|
|---|
| 89 | super.name(name);
|
|---|
| 90 |
|
|---|
| 91 | this.isRootCmd ||
|
|---|
| 92 | (this._cmd._cmdsByName[name] = this);
|
|---|
| 93 |
|
|---|
| 94 | return this;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Create new or add existing subcommand for current command.
|
|---|
| 99 | *
|
|---|
| 100 | * @param {COA.Cmd} [cmd] existing command instance
|
|---|
| 101 | * @returns {COA.Cmd} new subcommand instance
|
|---|
| 102 | */
|
|---|
| 103 | cmd(cmd) {
|
|---|
| 104 | return cmd?
|
|---|
| 105 | cmd._parent(this)
|
|---|
| 106 | : new Cmd(this);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Create option for current command.
|
|---|
| 111 | *
|
|---|
| 112 | * @returns {COA.Opt} new option instance
|
|---|
| 113 | */
|
|---|
| 114 | opt() {
|
|---|
| 115 | return new Opt(this);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Create argument for current command.
|
|---|
| 120 | *
|
|---|
| 121 | * @returns {COA.Opt} new argument instance
|
|---|
| 122 | */
|
|---|
| 123 | arg() {
|
|---|
| 124 | return new Arg(this);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Add (or set) action for current command.
|
|---|
| 129 | *
|
|---|
| 130 | * @param {Function} act - action function,
|
|---|
| 131 | * invoked in the context of command instance
|
|---|
| 132 | * and has the parameters:
|
|---|
| 133 | * - {Object} opts - parsed options
|
|---|
| 134 | * - {String[]} args - parsed arguments
|
|---|
| 135 | * - {Object} res - actions result accumulator
|
|---|
| 136 | * It can return rejected promise by Cmd.reject (in case of error)
|
|---|
| 137 | * or any other value treated as result.
|
|---|
| 138 | * @param {Boolean} [force=false] flag for set action instead add to existings
|
|---|
| 139 | * @returns {COA.Cmd} - this instance (for chainability)
|
|---|
| 140 | */
|
|---|
| 141 | act(act, force) {
|
|---|
| 142 | if(!act) return this;
|
|---|
| 143 |
|
|---|
| 144 | (!this._act || force) && (this._act = []);
|
|---|
| 145 | this._act.push(act);
|
|---|
| 146 |
|
|---|
| 147 | return this;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Make command "helpful", i.e. add -h --help flags for print usage.
|
|---|
| 152 | *
|
|---|
| 153 | * @returns {COA.Cmd} - this instance (for chainability)
|
|---|
| 154 | */
|
|---|
| 155 | helpful() {
|
|---|
| 156 | return this.opt()
|
|---|
| 157 | .name('help')
|
|---|
| 158 | .title('Help')
|
|---|
| 159 | .short('h')
|
|---|
| 160 | .long('help')
|
|---|
| 161 | .flag()
|
|---|
| 162 | .only()
|
|---|
| 163 | .act(function() {
|
|---|
| 164 | return this.usage();
|
|---|
| 165 | })
|
|---|
| 166 | .end();
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Adds shell completion to command, adds "completion" subcommand,
|
|---|
| 171 | * that makes all the magic.
|
|---|
| 172 | * Must be called only on root command.
|
|---|
| 173 | *
|
|---|
| 174 | * @returns {COA.Cmd} - this instance (for chainability)
|
|---|
| 175 | */
|
|---|
| 176 | completable() {
|
|---|
| 177 | return this.cmd()
|
|---|
| 178 | .name('completion')
|
|---|
| 179 | .apply(completion)
|
|---|
| 180 | .end();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * Allow command to be extendable by external node.js modules.
|
|---|
| 185 | *
|
|---|
| 186 | * @param {String} [pattern] Pattern of node.js module to find subcommands at.
|
|---|
| 187 | * @returns {COA.Cmd} - this instance (for chainability)
|
|---|
| 188 | */
|
|---|
| 189 | extendable(pattern) {
|
|---|
| 190 | this._ext = pattern || true;
|
|---|
| 191 | return this;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | _exit(msg, code) {
|
|---|
| 195 | return process.once('exit', function(exitCode) {
|
|---|
| 196 | msg && console[code === 0 ? 'log' : 'error'](msg);
|
|---|
| 197 | process.exit(code || exitCode || 0);
|
|---|
| 198 | });
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /**
|
|---|
| 202 | * Build full usage text for current command instance.
|
|---|
| 203 | *
|
|---|
| 204 | * @returns {String} usage text
|
|---|
| 205 | */
|
|---|
| 206 | usage() {
|
|---|
| 207 | const res = [];
|
|---|
| 208 |
|
|---|
| 209 | this._title && res.push(this._fullTitle());
|
|---|
| 210 |
|
|---|
| 211 | res.push('', 'Usage:');
|
|---|
| 212 |
|
|---|
| 213 | this._cmds.length
|
|---|
| 214 | && res.push([
|
|---|
| 215 | '', '', chalk.redBright(this._fullName()), chalk.blueBright('COMMAND'),
|
|---|
| 216 | chalk.greenBright('[OPTIONS]'), chalk.magentaBright('[ARGS]')
|
|---|
| 217 | ].join(' '));
|
|---|
| 218 |
|
|---|
| 219 | (this._opts.length + this._args.length)
|
|---|
| 220 | && res.push([
|
|---|
| 221 | '', '', chalk.redBright(this._fullName()),
|
|---|
| 222 | chalk.greenBright('[OPTIONS]'), chalk.magentaBright('[ARGS]')
|
|---|
| 223 | ].join(' '));
|
|---|
| 224 |
|
|---|
| 225 | res.push(
|
|---|
| 226 | this._usages(this._cmds, 'Commands'),
|
|---|
| 227 | this._usages(this._opts, 'Options'),
|
|---|
| 228 | this._usages(this._args, 'Arguments')
|
|---|
| 229 | );
|
|---|
| 230 |
|
|---|
| 231 | return res.join(EOL);
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | _usage() {
|
|---|
| 235 | return chalk.blueBright(this._name) + ' : ' + this._title;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | _usages(os, title) {
|
|---|
| 239 | if(!os.length) return;
|
|---|
| 240 |
|
|---|
| 241 | return ['', title + ':']
|
|---|
| 242 | .concat(os.map(o => ` ${o._usage()}`))
|
|---|
| 243 | .join(EOL);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | _fullTitle() {
|
|---|
| 247 | return `${this.isRootCmd? '' : this._cmd._fullTitle() + EOL}${this._title}`;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | _fullName() {
|
|---|
| 251 | return `${this.isRootCmd? '' : this._cmd._fullName() + ' '}${PATH.basename(this._name)}`;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | _ejectOpt(opts, opt) {
|
|---|
| 255 | const pos = opts.indexOf(opt);
|
|---|
| 256 | if(pos === -1) return;
|
|---|
| 257 |
|
|---|
| 258 | return opts[pos]._arr?
|
|---|
| 259 | opts[pos] :
|
|---|
| 260 | opts.splice(pos, 1)[0];
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | _checkRequired(opts, args) {
|
|---|
| 264 | if(this._opts.some(opt => opt._only && opts.hasOwnProperty(opt._name))) return;
|
|---|
| 265 |
|
|---|
| 266 | const all = this._opts.concat(this._args);
|
|---|
| 267 | let i;
|
|---|
| 268 | while(i = all.shift())
|
|---|
| 269 | if(i._req && i._checkParsed(opts, args))
|
|---|
| 270 | return this.reject(i._requiredText());
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | _parseCmd(argv, unparsed) {
|
|---|
| 274 | unparsed || (unparsed = []);
|
|---|
| 275 |
|
|---|
| 276 | let i,
|
|---|
| 277 | optSeen = false;
|
|---|
| 278 | while(i = argv.shift()) {
|
|---|
| 279 | i.indexOf('-') || (optSeen = true);
|
|---|
| 280 |
|
|---|
| 281 | if(optSeen || !/^\w[\w-_]*$/.test(i)) {
|
|---|
| 282 | unparsed.push(i);
|
|---|
| 283 | continue;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | let pkg, cmd = this._cmdsByName[i];
|
|---|
| 287 | if(!cmd && this._ext) {
|
|---|
| 288 | if(this._ext === true) {
|
|---|
| 289 | pkg = i;
|
|---|
| 290 | let c = this;
|
|---|
| 291 | while(true) { // eslint-disable-line
|
|---|
| 292 | pkg = c._name + '-' + pkg;
|
|---|
| 293 | if(c.isRootCmd) break;
|
|---|
| 294 | c = c._cmd;
|
|---|
| 295 | }
|
|---|
| 296 | } else if(typeof this._ext === 'string')
|
|---|
| 297 | pkg = ~this._ext.indexOf('%s')?
|
|---|
| 298 | UTIL.format(this._ext, i) :
|
|---|
| 299 | this._ext + i;
|
|---|
| 300 |
|
|---|
| 301 | let cmdDesc;
|
|---|
| 302 | try {
|
|---|
| 303 | cmdDesc = require(pkg);
|
|---|
| 304 | } catch(e) {
|
|---|
| 305 | // Dummy
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | if(cmdDesc) {
|
|---|
| 309 | if(typeof cmdDesc === 'function') {
|
|---|
| 310 | this.cmd().name(i).apply(cmdDesc).end();
|
|---|
| 311 | } else if(typeof cmdDesc === 'object') {
|
|---|
| 312 | this.cmd(cmdDesc);
|
|---|
| 313 | cmdDesc.name(i);
|
|---|
| 314 | } else throw new Error('Error: Unsupported command declaration type, '
|
|---|
| 315 | + 'should be a function or COA.Cmd() object');
|
|---|
| 316 |
|
|---|
| 317 | cmd = this._cmdsByName[i];
|
|---|
| 318 | }
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | if(cmd) return cmd._parseCmd(argv, unparsed);
|
|---|
| 322 |
|
|---|
| 323 | unparsed.push(i);
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | return { cmd : this, argv : unparsed };
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | _parseOptsAndArgs(argv) {
|
|---|
| 330 | const opts = {},
|
|---|
| 331 | args = {},
|
|---|
| 332 | nonParsedOpts = this._opts.concat(),
|
|---|
| 333 | nonParsedArgs = this._args.concat();
|
|---|
| 334 |
|
|---|
| 335 | let res, i;
|
|---|
| 336 | while(i = argv.shift()) {
|
|---|
| 337 | if(i !== '--' && i[0] === '-') {
|
|---|
| 338 | const m = i.match(/^(--\w[\w-_]*)=(.*)$/);
|
|---|
| 339 | if(m) {
|
|---|
| 340 | i = m[1];
|
|---|
| 341 | this._optsByKey[i]._flag || argv.unshift(m[2]);
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | const opt = this._ejectOpt(nonParsedOpts, this._optsByKey[i]);
|
|---|
| 345 | if(!opt) return this.reject(`Unknown option: ${i}`);
|
|---|
| 346 |
|
|---|
| 347 | if(Q.isRejected(res = opt._parse(argv, opts))) return res;
|
|---|
| 348 |
|
|---|
| 349 | continue;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | i === '--' && (i = argv.splice(0));
|
|---|
| 353 | Array.isArray(i) || (i = [i]);
|
|---|
| 354 |
|
|---|
| 355 | let a;
|
|---|
| 356 | while(a = i.shift()) {
|
|---|
| 357 | let arg = nonParsedArgs.shift();
|
|---|
| 358 | if(!arg) return this.reject(`Unknown argument: ${a}`);
|
|---|
| 359 |
|
|---|
| 360 | arg._arr && nonParsedArgs.unshift(arg);
|
|---|
| 361 | if(Q.isRejected(res = arg._parse(a, args))) return res;
|
|---|
| 362 | }
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | return {
|
|---|
| 366 | opts : this._setDefaults(opts, nonParsedOpts),
|
|---|
| 367 | args : this._setDefaults(args, nonParsedArgs)
|
|---|
| 368 | };
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | _setDefaults(params, desc) {
|
|---|
| 372 | for(const item of desc)
|
|---|
| 373 | item._def !== undefined &&
|
|---|
| 374 | !params.hasOwnProperty(item._name) &&
|
|---|
| 375 | item._saveVal(params, item._def);
|
|---|
| 376 |
|
|---|
| 377 | return params;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | _processParams(params, desc) {
|
|---|
| 381 | const notExists = [];
|
|---|
| 382 |
|
|---|
| 383 | for(const item of desc) {
|
|---|
| 384 | const n = item._name;
|
|---|
| 385 |
|
|---|
| 386 | if(!params.hasOwnProperty(n)) {
|
|---|
| 387 | notExists.push(item);
|
|---|
| 388 | continue;
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | const vals = Array.isArray(params[n])? params[n] : [params[n]];
|
|---|
| 392 | delete params[n];
|
|---|
| 393 |
|
|---|
| 394 | let res;
|
|---|
| 395 | for(const v of vals)
|
|---|
| 396 | if(Q.isRejected(res = item._saveVal(params, v)))
|
|---|
| 397 | return res;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | return this._setDefaults(params, notExists);
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | _parseArr(argv) {
|
|---|
| 404 | return Q.when(this._parseCmd(argv), p =>
|
|---|
| 405 | Q.when(p.cmd._parseOptsAndArgs(p.argv), r => ({
|
|---|
| 406 | cmd : p.cmd,
|
|---|
| 407 | opts : r.opts,
|
|---|
| 408 | args : r.args
|
|---|
| 409 | })));
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | _do(inputPromise) {
|
|---|
| 413 | return Q.when(inputPromise, input => {
|
|---|
| 414 | return [this._checkRequired]
|
|---|
| 415 | .concat(input.cmd._act || [])
|
|---|
| 416 | .reduce((res, act) =>
|
|---|
| 417 | Q.when(res, prev => act.call(input.cmd, input.opts, input.args, prev)),
|
|---|
| 418 | undefined);
|
|---|
| 419 | });
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | /**
|
|---|
| 423 | * Parse arguments from simple format like NodeJS process.argv
|
|---|
| 424 | * and run ahead current program, i.e. call process.exit when all actions done.
|
|---|
| 425 | *
|
|---|
| 426 | * @param {String[]} argv - arguments
|
|---|
| 427 | * @returns {COA.Cmd} - this instance (for chainability)
|
|---|
| 428 | */
|
|---|
| 429 | run(argv) {
|
|---|
| 430 | argv || (argv = process.argv.slice(2));
|
|---|
| 431 |
|
|---|
| 432 | const cb = code =>
|
|---|
| 433 | res => res?
|
|---|
| 434 | this._exit(res.stack || res.toString(), (res.hasOwnProperty('exitCode')? res.exitCode : code) || 0) :
|
|---|
| 435 | this._exit();
|
|---|
| 436 |
|
|---|
| 437 | Q.when(this.do(argv), cb(0), cb(1)).done();
|
|---|
| 438 |
|
|---|
| 439 | return this;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | /**
|
|---|
| 443 | * Invoke specified (or current) command using provided
|
|---|
| 444 | * options and arguments.
|
|---|
| 445 | *
|
|---|
| 446 | * @param {String|String[]} [cmds] - subcommand to invoke (optional)
|
|---|
| 447 | * @param {Object} [opts] - command options (optional)
|
|---|
| 448 | * @param {Object} [args] - command arguments (optional)
|
|---|
| 449 | * @returns {Q.Promise}
|
|---|
| 450 | */
|
|---|
| 451 | invoke(cmds, opts, args) {
|
|---|
| 452 | cmds || (cmds = []);
|
|---|
| 453 | opts || (opts = {});
|
|---|
| 454 | args || (args = {});
|
|---|
| 455 | typeof cmds === 'string' && (cmds = cmds.split(' '));
|
|---|
| 456 |
|
|---|
| 457 | if(arguments.length < 3 && !Array.isArray(cmds)) {
|
|---|
| 458 | args = opts;
|
|---|
| 459 | opts = cmds;
|
|---|
| 460 | cmds = [];
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| 463 | return Q.when(this._parseCmd(cmds), p => {
|
|---|
| 464 | if(p.argv.length)
|
|---|
| 465 | return this.reject(`Unknown command: ${cmds.join(' ')}`);
|
|---|
| 466 |
|
|---|
| 467 | return Q.all([
|
|---|
| 468 | this._processParams(opts, this._opts),
|
|---|
| 469 | this._processParams(args, this._args)
|
|---|
| 470 | ]).spread((_opts, _args) =>
|
|---|
| 471 | this._do({
|
|---|
| 472 | cmd : p.cmd,
|
|---|
| 473 | opts : _opts,
|
|---|
| 474 | args : _args
|
|---|
| 475 | })
|
|---|
| 476 | .fail(res => (res && res.exitCode === 0)?
|
|---|
| 477 | res.toString() :
|
|---|
| 478 | this.reject(res)));
|
|---|
| 479 | });
|
|---|
| 480 | }
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | /**
|
|---|
| 484 | * Convenient function to run command from tests.
|
|---|
| 485 | *
|
|---|
| 486 | * @param {String[]} argv - arguments
|
|---|
| 487 | * @returns {Q.Promise}
|
|---|
| 488 | */
|
|---|
| 489 | Cmd.prototype.do = function(argv) {
|
|---|
| 490 | return this._do(this._parseArr(argv || []));
|
|---|
| 491 | };
|
|---|
| 492 |
|
|---|
| 493 | module.exports = Cmd;
|
|---|