Skip to content

Commit

Permalink
Merge branch 'pr/545'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyelee committed Jul 5, 2016
2 parents c86ba2a + ad46a4d commit c6236d9
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 10 deletions.
6 changes: 3 additions & 3 deletions examples/pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var program = require('..');

program
.version('0.0.1')
.command('install [name]', 'install one or more packages')
.command('search [query]', 'search with optional query')
.command('install [name]', 'install one or more packages').alias('i')
.command('search [query]', 'search with optional query').alias('s')
.command('list', 'list packages installed')
.command('publish', 'publish the package')
.command('publish', 'publish the package').alias('p')
.parse(process.argv);

// here .command() is invoked with a description,
Expand Down
25 changes: 22 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,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);
}

Expand Down Expand Up @@ -850,8 +863,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;
};

Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ var program = require('../../');

program
.version('0.0.1')
.command('install [name]', 'install one or more packages')
.command('search [query]', 'search with optional query')
.command('cache', 'actions dealing with the cache')
.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')
.command('publish', 'publish or update package')
.command('publish', 'publish or update package').alias('p')
.command('default', 'default command', {noHelp: true, isDefault: true})
.parse(process.argv);
20 changes: 20 additions & 0 deletions test/test.command.alias.help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var 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|');
17 changes: 17 additions & 0 deletions test/test.command.executableSubcommandAlias.help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var exec = require('child_process').exec
, path = require('path')
, should = require('should');



var bin = path.join(__dirname, './fixtures/pm')

// success case
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');
stdout.should.containEql('publish|p');
stdout.should.not.containEql('pm|');
});
28 changes: 28 additions & 0 deletions test/test.command.executableSubcommandAlias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var exec = require('child_process').exec
, path = require('path')
, should = require('should');

var bin = path.join(__dirname, './fixtures/pm')

// success case
exec(bin + ' i', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

// subcommand bin file with explicit extension
exec(bin + ' p', function (error, stdout, stderr) {
stdout.should.equal('publish\n');
});

// spawn EACCES
exec(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 install
var bin = path.join(__dirname, './fixtures/pmlink')
// success case
exec(bin + ' i', function (error, stdout, stderr) {
stdout.should.equal('install\n');
});

0 comments on commit c6236d9

Please sign in to comment.