Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address issues from TypeScript checking of javascript #1194

Merged
merged 1 commit into from
Feb 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const spawn = require('child_process').spawn;
const path = require('path');
const fs = require('fs');

// @ts-check

class Option {
/**
* Initialize a new `Option` with the given `flags` and `description`.
Expand All @@ -22,10 +24,11 @@ class Option {
this.optional = flags.indexOf('[') >= 0; // A value is optional when the option is specified.
this.mandatory = false; // The option must have a value after parsing, which usually means it must be specified on command line.
this.negate = flags.indexOf('-no-') !== -1;
flags = flags.split(/[ ,|]+/);
if (flags.length > 1 && !/^[[<]/.test(flags[1])) this.short = flags.shift();
this.long = flags.shift();
const flagParts = flags.split(/[ ,|]+/);
if (flagParts.length > 1 && !/^[[<]/.test(flagParts[1])) this.short = flagParts.shift();
this.long = flagParts.shift();
this.description = description || '';
this.defaultValue = undefined;
}

/**
Expand Down Expand Up @@ -83,6 +86,7 @@ class CommanderError extends Error {
this.name = this.constructor.name;
this.code = code;
this.exitCode = exitCode;
this.nestedError = undefined;
}
}

Expand All @@ -98,6 +102,7 @@ class Command extends EventEmitter {
super();
this.commands = [];
this.options = [];
this.parent = null;
this._allowUnknownOption = false;
this._args = [];
this.rawArgs = null;
Expand All @@ -112,6 +117,7 @@ class Command extends EventEmitter {
this._executableFile = null; // custom name for executable
this._defaultCommandName = null;
this._exitCallback = null;
this._alias = null;

this._noHelp = false;
this._helpFlags = '-h, --help';
Expand Down Expand Up @@ -634,7 +640,7 @@ class Command extends EventEmitter {
*
* @param {string[]} [argv] - optional, defaults to process.argv
* @param {Object} [parseOptions] - optionally specify style of options with from: node/user/electron
* @param {string} parseOptions.from - where the args are from: 'node', 'user', 'electron'
* @param {string} [parseOptions.from] - where the args are from: 'node', 'user', 'electron'
* @return {Command} for chaining
* @api public
*/
Expand All @@ -648,6 +654,7 @@ class Command extends EventEmitter {
// Default to using process.argv
if (argv === undefined) {
argv = process.argv;
// @ts-ignore
if (process.versions && process.versions.electron) {
parseOptions.from = 'electron';
}
Expand All @@ -663,6 +670,7 @@ class Command extends EventEmitter {
userArgs = argv.slice(2);
break;
case 'electron':
// @ts-ignore
if (process.defaultApp) {
this._scriptPath = argv[1];
userArgs = argv.slice(2);
Expand Down Expand Up @@ -780,6 +788,7 @@ class Command extends EventEmitter {

const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'];
signals.forEach((signal) => {
// @ts-ignore
process.on(signal, () => {
if (proc.killed === false && proc.exitCode === null) {
proc.kill(signal);
Expand All @@ -798,11 +807,13 @@ class Command extends EventEmitter {
});
}
proc.on('error', (err) => {
// @ts-ignore
if (err.code === 'ENOENT') {
const executableMissing = `'${bin}' does not exist
- if '${subcommand._name}' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
- if the default executable name is not suitable, use the executableFile option to supply a custom name`;
throw new Error(executableMissing);
// @ts-ignore
} else if (err.code === 'EACCES') {
throw new Error(`'${bin}' not executable`);
}
Expand Down Expand Up @@ -1119,7 +1130,6 @@ class Command extends EventEmitter {
/**
* Unknown command.
*
* @param {string} flag
* @api private
*/

Expand All @@ -1145,7 +1155,7 @@ class Command extends EventEmitter {
* @param {string} str
* @param {string} [flags]
* @param {string} [description]
* @return {Command} for chaining
* @return {Command | string} this for chaining
* @api public
*/

Expand Down