diff options
Diffstat (limited to 'node_modules/commander/index.js')
-rw-r--r-- | node_modules/commander/index.js | 103 |
1 files changed, 65 insertions, 38 deletions
diff --git a/node_modules/commander/index.js b/node_modules/commander/index.js index a19c05d2e..d5dbe1868 100644 --- a/node_modules/commander/index.js +++ b/node_modules/commander/index.js @@ -4,7 +4,6 @@ var EventEmitter = require('events').EventEmitter; var spawn = require('child_process').spawn; -var readlink = require('graceful-readlink').readlinkSync; var path = require('path'); var dirname = path.dirname; var basename = path.basename; @@ -302,8 +301,8 @@ Command.prototype.action = function(fn) { }; var parent = this.parent || this; var name = parent === this ? '*' : this._name; - parent.on(name, listener); - if (this._alias) parent.on(this._alias, listener); + parent.on('command:' + name, listener); + if (this._alias) parent.on('command:' + this._alias, listener); return this; }; @@ -350,8 +349,8 @@ Command.prototype.action = function(fn) { * * @param {String} flags * @param {String} description - * @param {Function|Mixed} fn or default - * @param {Mixed} defaultValue + * @param {Function|*} [fn] or default + * @param {*} [defaultValue] * @return {Command} for chaining * @api public */ @@ -390,7 +389,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue) { // when it's passed assign the value // and conditionally invoke the callback - this.on(oname, function(val) { + this.on('option:' + oname, function(val) { // coercion if (null !== val && fn) val = fn(val, undefined === self[name] ? defaultValue @@ -459,11 +458,24 @@ Command.prototype.parse = function(argv) { // executable sub-commands var name = result.args[0]; + + var aliasCommand = null; + // check alias of sub commands + if (name) { + aliasCommand = this.commands.filter(function(command) { + return command.alias() === name; + })[0]; + } + if (this._execs[name] && typeof this._execs[name] != "function") { return this.executeSubCommand(argv, args, parsed.unknown); + } else if (aliasCommand) { + // is alias of a subCommand + args[0] = aliasCommand._name; + return this.executeSubCommand(argv, args, parsed.unknown); } else if (this.defaultExecutable) { // use the default subcommand - args.unshift(name = this.defaultExecutable); + args.unshift(this.defaultExecutable); return this.executeSubCommand(argv, args, parsed.unknown); } @@ -500,7 +512,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { // In case of globally installed, get the base dir where executable // subcommand file should be located at var baseDir - , link = readlink(f); + , link = fs.lstatSync(f).isSymbolicLink() ? fs.readlinkSync(f) : f; // when symbolink is relative path if (link !== f && link.charAt(0) !== '/') { @@ -525,7 +537,7 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { var proc; if (process.platform !== 'win32') { if (isExplicitJS) { - args.unshift(localBin); + args.unshift(bin); // add executable arguments to spawn args = (process.execArgv || []).concat(args); @@ -534,10 +546,18 @@ Command.prototype.executeSubCommand = function(argv, args, unknown) { proc = spawn(bin, args, { stdio: 'inherit', customFds: [0, 1, 2] }); } } else { - args.unshift(localBin); + args.unshift(bin); proc = spawn(process.execPath, args, { stdio: 'inherit'}); } + var signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP']; + signals.forEach(function(signal) { + process.on(signal, function(){ + if ((proc.killed === false) && (proc.exitCode === null)){ + proc.kill(signal); + } + }); + }); proc.on('close', process.exit.bind(process)); proc.on('error', function(err) { if (err.code == "ENOENT") { @@ -611,10 +631,10 @@ Command.prototype.parseArgs = function(args, unknown) { if (args.length) { name = args[0]; - if (this.listeners(name).length) { - this.emit(args.shift(), args, unknown); + if (this.listeners('command:' + name).length) { + this.emit('command:' + args.shift(), args, unknown); } else { - this.emit('*', args); + this.emit('command:*', args); } } else { outputHelpIfNecessary(this, unknown); @@ -668,13 +688,13 @@ Command.prototype.parseOptions = function(argv) { arg = argv[i]; // literal args after -- - if ('--' == arg) { - literal = true; + if (literal) { + args.push(arg); continue; } - if (literal) { - args.push(arg); + if ('--' == arg) { + literal = true; continue; } @@ -687,7 +707,7 @@ Command.prototype.parseOptions = function(argv) { if (option.required) { arg = argv[++i]; if (null == arg) return this.optionMissingArgument(option); - this.emit(option.name(), arg); + this.emit('option:' + option.name(), arg); // optional arg } else if (option.optional) { arg = argv[i+1]; @@ -696,10 +716,10 @@ Command.prototype.parseOptions = function(argv) { } else { ++i; } - this.emit(option.name(), arg); + this.emit('option:' + option.name(), arg); // bool } else { - this.emit(option.name()); + this.emit('option:' + option.name()); } continue; } @@ -810,7 +830,7 @@ Command.prototype.variadicArgNotLast = function(name) { * which will print the version number when passed. * * @param {String} str - * @param {String} flags + * @param {String} [flags] * @return {Command} for chaining * @api public */ @@ -820,7 +840,7 @@ Command.prototype.version = function(str, flags) { this._version = str; flags = flags || '-V, --version'; this.option(flags, 'output the version number'); - this.on('version', function() { + this.on('option:version', function() { process.stdout.write(str + '\n'); process.exit(0); }); @@ -850,8 +870,14 @@ Command.prototype.description = function(str) { */ Command.prototype.alias = function(alias) { - if (0 == arguments.length) return this._alias; - this._alias = alias; + var command = this; + if(this.commands.length !== 0) { + command = this.commands[this.commands.length - 1] + } + + if (arguments.length === 0) return command._alias; + + command._alias = alias; return this; }; @@ -879,15 +905,17 @@ Command.prototype.usage = function(str) { }; /** - * Get the name of the command + * Get or set the name of the command * - * @param {String} name + * @param {String} str * @return {String|Command} * @api public */ -Command.prototype.name = function() { - return this._name; +Command.prototype.name = function(str) { + if (0 === arguments.length) return this._name; + this._name = str; + return this; }; /** @@ -913,12 +941,11 @@ Command.prototype.largestOptionLength = function() { Command.prototype.optionHelp = function() { var width = this.largestOptionLength(); - // Prepend the help information - return [pad('-h, --help', width) + ' ' + 'output usage information'] - .concat(this.options.map(function(option) { - return pad(option.flags, width) + ' ' + option.description; - })) - .join('\n'); + // Append the help information + return this.options.map(function(option) { + return pad(option.flags, width) + ' ' + option.description; + }).concat([pad('-h, --help', width) + ' ' + 'output usage information']) + .join('\n'); }; /** @@ -943,7 +970,7 @@ Command.prototype.commandHelp = function() { + (cmd._alias ? '|' + cmd._alias : '') + (cmd.options.length ? ' [options]' : '') + ' ' + args - , cmd.description() + , cmd._description ]; }); @@ -994,17 +1021,17 @@ Command.prototype.helpInformation = function() { if (commandHelp) cmds = [commandHelp]; var options = [ - ' Options:' + '' + , ' Options:' , '' , '' + this.optionHelp().replace(/^/gm, ' ') , '' - , '' ]; return usage - .concat(cmds) .concat(desc) .concat(options) + .concat(cmds) .join('\n'); }; |