Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyelee committed Jul 5, 2016
1 parent afd6f49 commit ad46a4d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
28 changes: 17 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,18 +459,24 @@ Command.prototype.parse = function(argv) {

// executable sub-commands
var name = result.args[0];
var aliasCommand = this.commands.filter(function(command) {
var _alias = command.alias();
return _alias !== undefined && _alias === name;
})[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 !== undefined) {
} 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);
}

Expand Down Expand Up @@ -857,13 +863,13 @@ Command.prototype.description = function(str) {
*/

Command.prototype.alias = function(alias) {
var command = undefined;
if(this.commands.length == 0) {
command = this;
} else {
var command = this;
if(this.commands.length !== 0) {
command = this.commands[this.commands.length - 1]
}
if (0 == arguments.length) return command._alias;

if (arguments.length === 0) return command._alias;

command._alias = alias;
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ program
.command('install [name]', 'install one or more packages').alias('i')
.command('search [query]', 'search with optional query').alias('s')
.command('cache', 'actions dealing with the cache').alias('c')
.command('list', 'list packages installed').alias('l')
.command('list', 'list packages installed')
.command('publish', 'publish or update package').alias('p')
.command('default', 'default command', {noHelp: true, isDefault: true})
.parse(process.argv);
2 changes: 1 addition & 1 deletion test/test.command.executableSubcommandAlias.help.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exec(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|l');
stdout.should.containEql('list');
stdout.should.containEql('publish|p');
stdout.should.not.containEql('pm|');
});
10 changes: 1 addition & 9 deletions test/test.command.executableSubcommandAlias.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@ var exec = require('child_process').exec
, path = require('path')
, should = require('should');



var bin = path.join(__dirname, './fixtures/pm')
// not exist
exec(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 case
exec(bin + ' i', function (error, stdout, stderr) {
Expand All @@ -24,7 +16,7 @@ exec(bin + ' p', function (error, stdout, stderr) {

// spawn EACCES
exec(bin + ' s', function (error, stdout, stderr) {
// TODO error info are not the same in between <v0.10 and v0.12
// error info are not the same in between <v0.10 and v0.12
should.notEqual(0, stderr.length);
});

Expand Down

0 comments on commit ad46a4d

Please sign in to comment.