Merge branch 'pr/545'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by zhiyelee · committed by zhiyelee · parents c86ba2a, ad46a4d · Browse files · 6 files·+94 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+94 added−10 removed
- tests:+69 added−4 removed
- other:+25 added−6 removed
Provenance
- Commit
c6236d9- Tree
1567bee- Author
zhiyelee <zhiyelee@gmail.com>- Committer
zhiyelee <zhiyelee@gmail.com>- Parent 1
c86ba2a- Parent 2
ad46a4d
6 files changed+94 added−10 removed
modified:examples/pm+3 added-3 removed
2 unmodified lines3 var program = require('..');4 5 program6 .version('0.0.1')7- .command('install [name]', 'install one or more packages')8- .command('search [query]', 'search with optional query')7+ .command('install [name]', 'install one or more packages').alias('i')8+ .command('search [query]', 'search with optional query').alias('s')9 .command('list', 'list packages installed')10- .command('publish', 'publish the package')10+ .command('publish', 'publish the package').alias('p')11 .parse(process.argv);12 13 // here .command() is invoked with a description,14 // and no .action(callback) calls to handle sub-commands.13 unmodified linesmodified:test/fixtures/pm+4 added-4 removed
2 unmodified lines3 var program = require('../../');4 5 program6 .version('0.0.1')7- .command('install [name]', 'install one or more packages')8- .command('search [query]', 'search with optional query')9- .command('cache', 'actions dealing with the cache')7+ .command('install [name]', 'install one or more packages').alias('i')8+ .command('search [query]', 'search with optional query').alias('s')9+ .command('cache', 'actions dealing with the cache').alias('c')10 .command('list', 'list packages installed')11- .command('publish', 'publish or update package')11+ .command('publish', 'publish or update package').alias('p')12 .command('default', 'default command', {noHelp: true, isDefault: true})13 .parse(process.argv);added:test/test.command.alias.help.js+20 added
1234567891011121314151617181920var program = require('../') , should = require('should');
program .command('info [thing]') .alias('i') .action(function () { });
program .command('save [file]') .alias('s') .action(function() { });
program.parse(['node', 'test']);
program.commandHelp().should.containEql('info|i');program.commandHelp().should.containEql('save|s');program.commandHelp().should.not.containEql('test|');added:test/test.command.executableSubcommandAlias.help.js+17 added
1234567891011121314151617var exec = require('child_process').exec , path = require('path') , should = require('should');
var bin = path.join(__dirname, './fixtures/pm')
// success caseexec(bin + ' help', function (error, stdout, stderr) { stdout.should.containEql('install|i'); stdout.should.containEql('search|s'); stdout.should.containEql('cache|c'); stdout.should.containEql('list'); stdout.should.containEql('publish|p'); stdout.should.not.containEql('pm|');});added:test/test.command.executableSubcommandAlias.js+28 added
12345678910111213141516171819202122232425262728var exec = require('child_process').exec , path = require('path') , should = require('should');
var bin = path.join(__dirname, './fixtures/pm')
// success caseexec(bin + ' i', function (error, stdout, stderr) { stdout.should.equal('install\n');});
// subcommand bin file with explicit extensionexec(bin + ' p', function (error, stdout, stderr) { stdout.should.equal('publish\n');});
// spawn EACCESexec(bin + ' s', function (error, stdout, stderr) { // error info are not the same in between <v0.10 and v0.12 should.notEqual(0, stderr.length);});
// when `bin` is a symbol link for mocking global installvar bin = path.join(__dirname, './fixtures/pmlink')// success caseexec(bin + ' i', function (error, stdout, stderr) { stdout.should.equal('install\n');});modified:index.js+22 added-3 removed
457 unmodified lines458 var result = this.parseArgs(this.args, parsed.unknown);459 460 // executable sub-commands461 var name = result.args[0];462+463+ var aliasCommand = null;464+ // check alias of sub commands465+ if (name) {466+ aliasCommand = this.commands.filter(function(command) {467+ return command.alias() === name;468+ })[0];469+ }470+471 if (this._execs[name] && typeof this._execs[name] != "function") {472 return this.executeSubCommand(argv, args, parsed.unknown);473+ } else if (aliasCommand) {474+ // is alias of a subCommand475+ args[0] = aliasCommand._name;476+ return this.executeSubCommand(argv, args, parsed.unknown);477 } else if (this.defaultExecutable) {478 // use the default subcommand466- args.unshift(name = this.defaultExecutable);479+ args.unshift(this.defaultExecutable);480 return this.executeSubCommand(argv, args, parsed.unknown);481 }482 483 return result;378 unmodified lines862 * @api public863 */864 865 Command.prototype.alias = function(alias) {853- if (0 == arguments.length) return this._alias;854- this._alias = alias;866+ var command = this;867+ if(this.commands.length !== 0) {868+ command = this.commands[this.commands.length - 1]869+ }870+871+ if (arguments.length === 0) return command._alias;872+873+ command._alias = alias;874 return this;875 };876 877 /**252 unmodified lines