Merge branch 'feature_specify_subcommand' of https://github.com/abetomo/commander.js into abetomo-feature_specify_subcommand
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by John Gee · parents 631dfdc, 5f8ef7b · Browse files · 3 files·+62 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+62 added−2 removed
- tests:+46 added−0 removed
- other:+16 added−2 removed
Provenance
- Commit
669272a- Tree
6b93d8a- Author
John Gee <john@ruru.gen.nz>- Committer
John Gee <john@ruru.gen.nz>- Parent 1
631dfdc- Parent 2
5f8ef7b
3 files changed+62 added−2 removed
modified:test/fixtures/pm+2 added
9 unmodified lines10 .command('list', 'list packages installed')11 .command('listen', 'listen for supported signal events').alias('l')12 .command('publish', 'publish or update package').alias('p')13 .command('default', 'default command', {noHelp: true, isDefault: true})14+ .command('specifyInstall', 'specify install subcommand', {subcommand: 'pm-install'})15+ .command('specifyPublish', 'specify publish subcommand', {subcommand: 'pm-publish'})16 .parse(process.argv);added:test/test.command.executableSubcommandSpecifySubcommand.js+44 added
1234567891011121314151617181920212223242526272829303132333435363738394041424344var exec = require('child_process').exec , path = require('path') , should = require('should');
var bin = path.join(__dirname, './fixtures/pm')// success caseexec(bin + ' specifyInstall', function(error, stdout, stderr) { // Optionally specify `pm-install` stdout.should.equal('install\n');});exec(bin + ' specifyPublish', function(error, stdout, stderr) { // Optionally specify `pm-publish` stdout.should.equal('publish\n');});
// not existexec(bin + ' list', function (error, stdout, stderr) { //stderr.should.equal('\n pm-list(1) does not exist, try --help\n\n'); should.notEqual(0, stderr.length);});
// success caseexec(bin + ' install', function (error, stdout, stderr) { stdout.should.equal('install\n');});
// subcommand bin file with explicit extensionexec(bin + ' publish', function (error, stdout, stderr) { stdout.should.equal('publish\n');});
// spawn EACCESexec(bin + ' search', function (error, stdout, stderr) { should.notEqual(0, stderr.length);});
// when `bin` is a symbol link for mocking global installvar bin = path.join(__dirname, './fixtures/pmlink')// success caseexec(bin + ' install', function (error, stdout, stderr) { stdout.should.equal('install\n');});modified:index.js+16 added-2 removed
180 unmodified lines181 this._execs[cmd._name] = true;182 if (opts.isDefault) this.defaultExecutable = cmd._name;183 }184 cmd._noHelp = !!opts.noHelp;185+ cmd._specifySubcommand = opts.subcommand;186 this.commands.push(cmd);187 cmd.parseExpectedArgs(args);188 cmd.parent = this;189 296 unmodified lines486 return command.alias() === name;487 })[0];488 }489 490+ var specifySubcommand = null;491+ if (name) {492+ specifySubcommand = this.commands.filter(function(command) {493+ return command._name === name && command._specifySubcommand != null;494+ })[0];495+ }496+497+ if (specifySubcommand) {498+ return this.executeSubCommand(argv, args, parsed.unknown, specifySubcommand._specifySubcommand);489- if (this._execs[name] && typeof this._execs[name] !== 'function') {499+ } else if (this._execs[name] && typeof this._execs[name] !== 'function') {500 return this.executeSubCommand(argv, args, parsed.unknown);501 } else if (aliasCommand) {502 // is alias of a subCommand503 args[0] = aliasCommand._name;12 unmodified lines516 *517 * @param {Array} argv518 * @param {Array} args519 * @param {Array} unknown520+ * @param {String} specifySubcommand521 * @api private522 */523 513-Command.prototype.executeSubCommand = function(argv, args, unknown) {524+Command.prototype.executeSubCommand = function(argv, args, unknown, specifySubcommand) {525 args = args.concat(unknown);526 527 if (!args.length) this.help();528 if (args[0] === 'help' && args.length === 1) this.help();7 unmodified lines536 // executable537 var f = argv[1];538 // name of the subcommand, link `pm-install`539 var bin = basename(f, path.extname(f)) + '-' + args[0];540+ if (specifySubcommand != null) {541+ bin = specifySubcommand;542+ }543 544 // In case of globally installed, get the base dir where executable545 // subcommand file should be located at546 var baseDir;701 unmodified lines