Support the alias of Git style sub-command
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by akira.takahashi · committed by akira.takahashi · parent ad2e539 · Browse files · 4 files·+59 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
4 files+59 added−10 removed
- tests:+41 added−5 removed
- other:+18 added−5 removed
Provenance
- Commit
81577a6- Tree
5562554- Author
akira.takahashi <rike422@gmail.com>- Committer
akira.takahashi <rike422@gmail.com>- Parent
ad2e539
4 files changed+59 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+5 added-5 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')10- .command('list', 'list packages installed')11- .command('publish', 'publish or update package')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').alias('l')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.executableSubcommandAlias.js+36 added
123456789101112131415161718192021222324252627282930313233343536var exec = require('child_process').exec , path = require('path') , should = require('should');
var bin = path.join(__dirname, './fixtures/pm')// not existexec(bin + ' l', function (error, stdout, stderr) { //stderr.should.equal('\n pm-list(1) does not exist, try --help\n\n'); // TODO error info are not the same in between <=v0.8 and later version should.notEqual(0, stderr.length);});
// 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) { // TODO 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+15 added-2 removed
457 unmodified lines458 var result = this.parseArgs(this.args, parsed.unknown);459 460 // executable sub-commands461 var name = result.args[0];462+ var aliasCommand = this.commands.filter(function(command) {463+ var _alias = command.alias();464+ return _alias !== undefined && _alias === name;465+ })[0];466 if (this._execs[name] && typeof this._execs[name] != "function") {467 return this.executeSubCommand(argv, args, parsed.unknown);468+ } else if (aliasCommand !== undefined) {469+ args[0] = aliasCommand._name;470+ return this.executeSubCommand(argv, args, parsed.unknown);471 } else if (this.defaultExecutable) {472 // use the default subcommand473 args.unshift(name = this.defaultExecutable);474 return this.executeSubCommand(argv, args, parsed.unknown);381 unmodified lines856 * @api public857 */858 859 Command.prototype.alias = function(alias) {860+ var command = undefined;861+ if(this.commands.length == 0) {862+ command = this;863+ } else {864+ command = this.commands[this.commands.length - 1]865+ }853- if (0 == arguments.length) return this._alias;854- this._alias = alias;866+ if (0 == arguments.length) return command._alias;867+ command._alias = alias;868 return this;869 };870 871 /**252 unmodified lines