Implement specification of subcommand
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by abetomo · committed by abetomo · parent 8bcc493 · 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
5f8ef7b- Tree
f3c8fb6- Author
abetomo <abe@enzou.tokyo>- Committer
abetomo <abe@enzou.tokyo>- Parent
8bcc493
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 294 unmodified lines484 return command.alias() === name;485 })[0];486 }487 488+ var specifySubcommand = null;489+ if (name) {490+ specifySubcommand = this.commands.filter(function(command) {491+ return command._name === name && command._specifySubcommand != null;492+ })[0];493+ }494+495+ if (specifySubcommand) {496+ return this.executeSubCommand(argv, args, parsed.unknown, specifySubcommand._specifySubcommand);487- if (this._execs[name] && typeof this._execs[name] !== 'function') {497+ } else if (this._execs[name] && typeof this._execs[name] !== 'function') {498 return this.executeSubCommand(argv, args, parsed.unknown);499 } else if (aliasCommand) {500 // is alias of a subCommand501 args[0] = aliasCommand._name;12 unmodified lines514 *515 * @param {Array} argv516 * @param {Array} args517 * @param {Array} unknown518+ * @param {String} specifySubcommand519 * @api private520 */521 511-Command.prototype.executeSubCommand = function(argv, args, unknown) {522+Command.prototype.executeSubCommand = function(argv, args, unknown, specifySubcommand) {523 args = args.concat(unknown);524 525 if (!args.length) this.help();526 if (args[0] === 'help' && args.length === 1) this.help();7 unmodified lines534 // executable535 var f = argv[1];536 // name of the subcommand, link `pm-install`537 var bin = basename(f, path.extname(f)) + '-' + args[0];538+ if (specifySubcommand != null) {539+ bin = specifySubcommand;540+ }541 542 // In case of globally installed, get the base dir where executable543 // subcommand file should be located at544 var baseDir,709 unmodified lines