Skip to content
Myr

Rework executeableFIle support

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- add to README - rename option - include support for executableFile for alias and default command - consistent use of node to launch javascript files

authored by John Gee · committed by John Gee · parent 2c4d743 · Browse files · 3 files+43 added−33 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+43 added−33 removed

  • tests:+3 added−3 removed
  • other:+40 added−30 removed

3 files+43 added−33 removed

modified:test/fixtures/pm+3 added-3 removed
8 unmodified lines
9 .command('cache', 'actions dealing with the cache').alias('c')10 .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'})13 .command('default', 'default command', { noHelp: true, isDefault: true })14 .command('specifyInstall', 'specify install subcommand', { executableFile: 'pm-install' })15 .command('specifyPublish', 'specify publish subcommand', { executableFile: 'pm-publish' })16 .parse(process.argv);
modified:index.js+37 added-30 removed
155 unmodified lines
156 cmd._helpFlags = this._helpFlags;157 cmd._helpDescription = this._helpDescription;158 cmd._helpShortFlag = this._helpShortFlag;159 cmd._helpLongFlag = this._helpLongFlag;160 cmd._specifySubcommand = opts.subcommand;160 cmd._executableFile = opts.executableFile; // Custom name for executable file161 this.commands.push(cmd);162 cmd.parseExpectedArgs(args);163 cmd.parent = this;164
294 unmodified lines
459 var result = this.parseArgs(this.args, parsed.unknown);460461 // executable sub-commands462 var name = result.args[0];463 var subCommand = null;464464 var aliasCommand = null;465 // check alias of sub commands465 // Look for subcommand466 if (name) {467 aliasCommand = this.commands.filter(function(command) {467 subCommand = this.commands.find(function(command) {468 return command._name === name;469 });470 }471472 // Look for alias473 if (!subCommand && name) {474 subCommand = this.commands.find(function(command) {475 return command.alias() === name;469 })[0];476 });477 if (subCommand) {478 name = subCommand._name;479 args[0] = name;480 }481 }482472 var specifySubcommand = null;473 if (name) {474 specifySubcommand = this.commands.filter(function(command) {475 return command._name === name && command._specifySubcommand != null;476 })[0];483 // Look for default subcommand484 if (!subCommand && this.defaultExecutable) {485 name = this.defaultExecutable;486 args.unshift(name);487 subCommand = this.commands.find(function(command) {488 return command._name === name;489 });490 }491479 if (specifySubcommand) {480 return this.executeSubCommand(argv, args, parsed.unknown, specifySubcommand._specifySubcommand);481 } else if (this._execs[name] && typeof this._execs[name] !== 'function') {482 return this.executeSubCommand(argv, args, parsed.unknown);483 } else if (aliasCommand) {484 // is alias of a subCommand485 args[0] = aliasCommand._name;486 return this.executeSubCommand(argv, args, parsed.unknown);487 } else if (this.defaultExecutable) {488 // use the default subcommand489 args.unshift(this.defaultExecutable);490 return this.executeSubCommand(argv, args, parsed.unknown);492 if (this._execs[name] && typeof this._execs[name] !== 'function') {493 return this.executeSubCommand(argv, args, parsed.unknown, subCommand ? subCommand._executableFile : undefined);494 }495496 return result;497};
7 unmodified lines
505 * @param {String} specifySubcommand506 * @api private507 */508506Command.prototype.executeSubCommand = function(argv, args, unknown, specifySubcommand) {509Command.prototype.executeSubCommand = function(argv, args, unknown, executableFile) {510 args = args.concat(unknown);511512 if (!args.length) this.help();513 if (args[0] === 'help' && args.length === 1) this.help();
3 unmodified lines
517 args[0] = args[1];518 args[1] = this._helpLongFlag;519 }520521 var isExplicitJS = false; // Whether to use node to launch "executable"522523 // executable519 var f = argv[1];520 // name of the subcommand, link `pm-install`521 var bin = basename(f, path.extname(f)) + '-' + args[0];522 if (specifySubcommand != null) {523 bin = specifySubcommand;524 var pm = argv[1];525 // name of the subcommand, like `pm-install`526 var bin = basename(pm, path.extname(pm)) + '-' + args[0];527 if (executableFile != null) {528 bin = executableFile;529 // Check for same extensions as we scan for below so get consistent launch behaviour.530 var executableExt = path.extname(executableFile);531 isExplicitJS = executableExt === '.js' || executableExt === '.ts' || executableExt === '.mjs';532 }533534 // In case of globally installed, get the base dir where executable535 // subcommand file should be located at536 var baseDir;537530 var resolvedLink = fs.realpathSync(f);538 var resolvedLink = fs.realpathSync(pm);539540 baseDir = dirname(resolvedLink);541542 // prefer local `./<bin>` to bin in the $PATH543 var localBin = path.join(baseDir, bin);544545 // whether bin file is a js script with explicit `.js` or `.ts` extension538 var isExplicitJS = false;546 if (exists(localBin + '.js')) {547 bin = localBin + '.js';548 isExplicitJS = true;549 } else if (exists(localBin + '.ts')) {
735 unmodified lines
modified:Readme.md+3 added
328 unmodified lines
329### Git-style executable (sub)commands330331When `.command()` is invoked with a description argument, this tells commander that you're going to use separate executables for sub-commands, much like `git(1)` and other popular tools.332Commander will search the executables in the directory of the entry script (like `./examples/pm`) with the name `program-subcommand`, like `pm-install`, `pm-search`.333You can specify a custom name with the `executableFile` configuration option.334335You handle the options for an executable (sub)command in the executable, and don't declare them at the top-level.336337```js
3 unmodified lines
341program342 .version('0.1.0')343 .command('install [name]', 'install one or more packages')344 .command('search [query]', 'search with optional query')345 .command('update', 'update installed packages', {executableFile: 'myUpdateSubCommand'})346 .command('list', 'list packages installed', {isDefault: true})347 .parse(process.argv);348```349350Configuration options can be passed with the call to `.command()`. Specifying `true` for `opts.noHelp` will remove the command from the generated help output. Specifying `true` for `opts.isDefault` will run the subcommand if no other subcommand is specified.351Specifying a name with `executableFile` will override the default constructed name.352353If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.354355## Automated --help
203 unmodified lines