Update checked-in dependencies

This commit is contained in:
github-actions[bot] 2021-07-28 10:18:36 +00:00
parent 25fef55830
commit 5fee2f2ed7
8 changed files with 337 additions and 587 deletions

310
node_modules/commander/lib/command.js generated vendored
View file

@ -19,13 +19,19 @@ class Command extends EventEmitter {
constructor(name) {
super();
/** @type {Command[]} */
this.commands = [];
/** @type {Option[]} */
this.options = [];
this.parent = null;
this._allowUnknownOption = false;
this._allowExcessArguments = true;
/** @type {Argument[]} */
this._args = [];
this.rawArgs = null;
/** @type {string[]} */
this.args = []; // cli args with options removed
this.rawArgs = [];
this.processedArgs = []; // like .args but after custom processing and collecting variadic
this._scriptPath = null;
this._name = name || '';
this._optionValues = {};
@ -42,6 +48,8 @@ class Command extends EventEmitter {
this._enablePositionalOptions = false;
this._passThroughOptions = false;
this._lifeCycleHooks = {}; // a hash of arrays
/** @type {boolean | string} */
this._showHelpAfterError = false;
// see .configureOutput() for docs
this._outputConfiguration = {
@ -65,25 +73,53 @@ class Command extends EventEmitter {
this._helpConfiguration = {};
}
/**
* Copy settings that are useful to have in common across root command and subcommands.
*
* (Used internally when adding a command using `.command()` so subcommands inherit parent settings.)
*
* @param {Command} sourceCommand
* @return {Command} returns `this` for executable command
*/
copyInheritedSettings(sourceCommand) {
this._outputConfiguration = sourceCommand._outputConfiguration;
this._hasHelpOption = sourceCommand._hasHelpOption;
this._helpFlags = sourceCommand._helpFlags;
this._helpDescription = sourceCommand._helpDescription;
this._helpShortFlag = sourceCommand._helpShortFlag;
this._helpLongFlag = sourceCommand._helpLongFlag;
this._helpCommandName = sourceCommand._helpCommandName;
this._helpCommandnameAndArgs = sourceCommand._helpCommandnameAndArgs;
this._helpCommandDescription = sourceCommand._helpCommandDescription;
this._helpConfiguration = sourceCommand._helpConfiguration;
this._exitCallback = sourceCommand._exitCallback;
this._storeOptionsAsProperties = sourceCommand._storeOptionsAsProperties;
this._combineFlagAndOptionalValue = sourceCommand._combineFlagAndOptionalValue;
this._allowExcessArguments = sourceCommand._allowExcessArguments;
this._enablePositionalOptions = sourceCommand._enablePositionalOptions;
this._showHelpAfterError = sourceCommand._showHelpAfterError;
return this;
}
/**
* Define a command.
*
* There are two styles of command: pay attention to where to put the description.
*
* Examples:
* @example
* // Command implemented using action handler (description is supplied separately to `.command`)
* program
* .command('clone <source> [destination]')
* .description('clone a repository into a newly created directory')
* .action((source, destination) => {
* console.log('clone command called');
* });
*
* // Command implemented using action handler (description is supplied separately to `.command`)
* program
* .command('clone <source> [destination]')
* .description('clone a repository into a newly created directory')
* .action((source, destination) => {
* console.log('clone command called');
* });
*
* // Command implemented using separate executable file (description is second parameter to `.command`)
* program
* .command('start <service>', 'start named service')
* .command('stop [service]', 'stop named service, or all if no name supplied');
* // Command implemented using separate executable file (description is second parameter to `.command`)
* program
* .command('start <service>', 'start named service')
* .command('stop [service]', 'stop named service, or all if no name supplied');
*
* @param {string} nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
* @param {Object|string} [actionOptsOrExecDesc] - configuration options (for action), or description (for executable)
@ -100,36 +136,19 @@ class Command extends EventEmitter {
}
opts = opts || {};
const [, name, args] = nameAndArgs.match(/([^ ]+) *(.*)/);
const cmd = this.createCommand(name);
const cmd = this.createCommand(name);
if (desc) {
cmd.description(desc);
cmd._executableHandler = true;
}
if (opts.isDefault) this._defaultCommandName = cmd._name;
cmd._outputConfiguration = this._outputConfiguration;
cmd._hidden = !!(opts.noHelp || opts.hidden); // noHelp is deprecated old name for hidden
cmd._hasHelpOption = this._hasHelpOption;
cmd._helpFlags = this._helpFlags;
cmd._helpDescription = this._helpDescription;
cmd._helpShortFlag = this._helpShortFlag;
cmd._helpLongFlag = this._helpLongFlag;
cmd._helpCommandName = this._helpCommandName;
cmd._helpCommandnameAndArgs = this._helpCommandnameAndArgs;
cmd._helpCommandDescription = this._helpCommandDescription;
cmd._helpConfiguration = this._helpConfiguration;
cmd._exitCallback = this._exitCallback;
cmd._storeOptionsAsProperties = this._storeOptionsAsProperties;
cmd._combineFlagAndOptionalValue = this._combineFlagAndOptionalValue;
cmd._allowExcessArguments = this._allowExcessArguments;
cmd._enablePositionalOptions = this._enablePositionalOptions;
cmd._executableFile = opts.executableFile || null; // Custom name for executable file, set missing to null to match constructor
if (args) cmd.arguments(args);
this.commands.push(cmd);
cmd.parent = this;
cmd.copyInheritedSettings(this);
if (desc) return this;
return cmd;
@ -181,14 +200,14 @@ class Command extends EventEmitter {
*
* The configuration properties are all functions:
*
* // functions to change where being written, stdout and stderr
* writeOut(str)
* writeErr(str)
* // matching functions to specify width for wrapping help
* getOutHelpWidth()
* getErrHelpWidth()
* // functions based on what is being written out
* outputError(str, write) // used for displaying errors, and not used for displaying help
* // functions to change where being written, stdout and stderr
* writeOut(str)
* writeErr(str)
* // matching functions to specify width for wrapping help
* getOutHelpWidth()
* getErrHelpWidth()
* // functions based on what is being written out
* outputError(str, write) // used for displaying errors, and not used for displaying help
*
* @param {Object} [configuration] - configuration options
* @return {Command|Object} `this` command for chaining, or stored configuration
@ -201,6 +220,18 @@ class Command extends EventEmitter {
return this;
}
/**
* Display the help or a custom message after an error occurs.
*
* @param {boolean|string} [displayHelp]
* @return {Command} `this` command for chaining
*/
showHelpAfterError(displayHelp = true) {
if (typeof displayHelp !== 'string') displayHelp = !!displayHelp;
this._showHelpAfterError = displayHelp;
return this;
}
/**
* Add a prepared subcommand.
*
@ -257,9 +288,8 @@ class Command extends EventEmitter {
* indicate this with <> around the name. Put [] around the name for an optional argument.
*
* @example
*
* program.argument('<input-file>');
* program.argument('[output-file]');
* program.argument('<input-file>');
* program.argument('[output-file]');
*
* @param {string} name
* @param {string} [description]
@ -284,8 +314,7 @@ class Command extends EventEmitter {
* See also .argument().
*
* @example
*
* program.arguments('<cmd> [env]');
* program.arguments('<cmd> [env]');
*
* @param {string} names
* @return {Command} `this` command for chaining
@ -417,14 +446,13 @@ Expecting one of '${allowedValues.join("', '")}'`);
/**
* Register callback `fn` for the command.
*
* Examples:
*
* program
* .command('help')
* .description('display verbose help')
* .action(function() {
* // output help here
* });
* @example
* program
* .command('help')
* .description('display verbose help')
* .action(function() {
* // output help here
* });
*
* @param {Function} fn
* @return {Command} `this` command for chaining
@ -563,41 +591,40 @@ Expecting one of '${allowedValues.join("', '")}'`);
* separated by comma, a pipe or space. The following are all valid
* all will output this way when `--help` is used.
*
* "-p, --pepper"
* "-p|--pepper"
* "-p --pepper"
* "-p, --pepper"
* "-p|--pepper"
* "-p --pepper"
*
* Examples:
* @example
* // simple boolean defaulting to undefined
* program.option('-p, --pepper', 'add pepper');
*
* // simple boolean defaulting to undefined
* program.option('-p, --pepper', 'add pepper');
* program.pepper
* // => undefined
*
* program.pepper
* // => undefined
* --pepper
* program.pepper
* // => true
*
* --pepper
* program.pepper
* // => true
* // simple boolean defaulting to true (unless non-negated option is also defined)
* program.option('-C, --no-cheese', 'remove cheese');
*
* // simple boolean defaulting to true (unless non-negated option is also defined)
* program.option('-C, --no-cheese', 'remove cheese');
* program.cheese
* // => true
*
* program.cheese
* // => true
* --no-cheese
* program.cheese
* // => false
*
* --no-cheese
* program.cheese
* // => false
* // required argument
* program.option('-C, --chdir <path>', 'change the working directory');
*
* // required argument
* program.option('-C, --chdir <path>', 'change the working directory');
* --chdir /tmp
* program.chdir
* // => "/tmp"
*
* --chdir /tmp
* program.chdir
* // => "/tmp"
*
* // optional argument
* program.option('-c, --cheese [type]', 'add cheese [marble]');
* // optional argument
* program.option('-c, --cheese [type]', 'add cheese [marble]');
*
* @param {string} flags
* @param {string} [description]
@ -630,11 +657,10 @@ Expecting one of '${allowedValues.join("', '")}'`);
/**
* Alter parsing of short flags with optional values.
*
* Examples:
*
* // for `.option('-f,--flag [value]'):
* .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour
* .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
* @example
* // for `.option('-f,--flag [value]'):
* program.combineFlagAndOptionalValue(true); // `-f80` is treated like `--flag=80`, this is the default behaviour
* program.combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
*
* @param {Boolean} [combine=true] - if `true` or omitted, an optional value can be specified directly after the flag.
*/
@ -803,11 +829,10 @@ Expecting one of '${allowedValues.join("', '")}'`);
* The default expectation is that the arguments are from node and have the application as argv[0]
* and the script being run in argv[1], with user parameters after that.
*
* Examples:
*
* program.parse(process.argv);
* program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
* program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
* @example
* program.parse(process.argv);
* program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
* program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
*
* @param {string[]} [argv] - optional, defaults to process.argv
* @param {Object} [parseOptions] - optionally specify style of options with from: node/user/electron
@ -830,11 +855,10 @@ Expecting one of '${allowedValues.join("', '")}'`);
* The default expectation is that the arguments are from node and have the application as argv[0]
* and the script being run in argv[1], with user parameters after that.
*
* Examples:
*
* await program.parseAsync(process.argv);
* await program.parseAsync(); // implicitly use process.argv and auto-detect node vs electron conventions
* await program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
* @example
* await program.parseAsync(process.argv);
* await program.parseAsync(); // implicitly use process.argv and auto-detect node vs electron conventions
* await program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
*
* @param {string[]} [argv]
* @param {Object} [parseOptions]
@ -976,13 +1000,34 @@ Expecting one of '${allowedValues.join("', '")}'`);
};
/**
* Package arguments (this.args) for passing to action handler based
* on declared arguments (this._args).
* Check this.args against expected this._args.
*
* @api private
*/
_getActionArguments() {
_checkNumberOfArguments() {
// too few
this._args.forEach((arg, i) => {
if (arg.required && this.args[i] == null) {
this.missingArgument(arg.name());
}
});
// too many
if (this._args.length > 0 && this._args[this._args.length - 1].variadic) {
return;
}
if (this.args.length > this._args.length) {
this._excessArguments(this.args);
}
};
/**
* Process this.args using this._args and save as this.processedArgs!
*
* @api private
*/
_processArguments() {
const myParseArg = (argument, value, previous) => {
// Extra processing for nice error message on parsing failure.
let parsedValue = value;
@ -1000,7 +1045,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
return parsedValue;
};
const actionArgs = [];
this._checkNumberOfArguments();
const processedArgs = [];
this._args.forEach((declaredArg, index) => {
let value = declaredArg.defaultValue;
if (declaredArg.variadic) {
@ -1021,9 +1068,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
value = myParseArg(declaredArg, value, declaredArg.defaultValue);
}
}
actionArgs[index] = value;
processedArgs[index] = value;
});
return actionArgs;
this.processedArgs = processedArgs;
}
/**
@ -1115,37 +1162,22 @@ Expecting one of '${allowedValues.join("', '")}'`);
this.unknownOption(parsed.unknown[0]);
}
};
const checkNumberOfArguments = () => {
// too few
this._args.forEach((arg, i) => {
if (arg.required && this.args[i] == null) {
this.missingArgument(arg.name());
}
});
// too many
if (this._args.length > 0 && this._args[this._args.length - 1].variadic) {
return;
}
if (this.args.length > this._args.length) {
this._excessArguments(this.args);
}
};
const commandEvent = `command:${this.name()}`;
if (this._actionHandler) {
checkForUnknownOptions();
checkNumberOfArguments();
this._processArguments();
let actionResult;
actionResult = this._chainOrCallHooks(actionResult, 'preAction');
actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this._getActionArguments()));
actionResult = this._chainOrCall(actionResult, () => this._actionHandler(this.processedArgs));
if (this.parent) this.parent.emit(commandEvent, operands, unknown); // legacy
actionResult = this._chainOrCallHooks(actionResult, 'postAction');
return actionResult;
}
if (this.parent && this.parent.listenerCount(commandEvent)) {
checkForUnknownOptions();
checkNumberOfArguments();
this._processArguments();
this.parent.emit(commandEvent, operands, unknown); // legacy
} else if (operands.length) {
if (this._findCommand('*')) { // legacy default command
@ -1158,14 +1190,14 @@ Expecting one of '${allowedValues.join("', '")}'`);
this.unknownCommand();
} else {
checkForUnknownOptions();
checkNumberOfArguments();
this._processArguments();
}
} else if (this.commands.length) {
// This command has subcommands and nothing hooked up at this level, so display help (and exit).
this.help({ error: true });
} else {
checkForUnknownOptions();
checkNumberOfArguments();
this._processArguments();
// fall through for caller to handle after calling .parse()
}
};
@ -1216,11 +1248,11 @@ Expecting one of '${allowedValues.join("', '")}'`);
*
* Examples:
*
* argv => operands, unknown
* --known kkk op => [op], []
* op --known kkk => [op], []
* sub --unknown uuu op => [sub], [--unknown uuu op]
* sub -- --unknown uuu op => [sub --unknown uuu op], []
* argv => operands, unknown
* --known kkk op => [op], []
* op --known kkk => [op], []
* sub --unknown uuu op => [sub], [--unknown uuu op]
* sub -- --unknown uuu op => [sub --unknown uuu op], []
*
* @param {String[]} argv
* @return {{operands: String[], unknown: String[]}}
@ -1370,6 +1402,12 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/
_displayError(exitCode, code, message) {
this._outputConfiguration.outputError(`${message}\n`, this._outputConfiguration.writeErr);
if (typeof this._showHelpAfterError === 'string') {
this._outputConfiguration.writeErr(`${this._showHelpAfterError}\n`);
} else if (this._showHelpAfterError) {
this._outputConfiguration.writeErr('\n');
this.outputHelp({ error: true });
}
this._exit(exitCode, code, message);
}
@ -1446,13 +1484,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
*/
unknownCommand() {
const partCommands = [this.name()];
for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
partCommands.unshift(parentCmd.name());
}
const fullCommand = partCommands.join(' ');
const message = `error: unknown command '${this.args[0]}'.` +
(this._hasHelpOption ? ` See '${fullCommand} ${this._helpLongFlag}'.` : '');
const message = `error: unknown command '${this.args[0]}'`;
this._displayError(1, 'commander.unknownCommand', message);
};
@ -1513,6 +1545,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
alias(alias) {
if (alias === undefined) return this._aliases[0]; // just return first, for backwards compatibility
/** @type {Command} */
let command = this;
if (this.commands.length !== 0 && this.commands[this.commands.length - 1]._executableHandler) {
// assume adding alias for last added executable subcommand, rather than this
@ -1629,14 +1662,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
}
const context = this._getHelpContext(contextOptions);
const groupListeners = [];
let command = this;
while (command) {
groupListeners.push(command); // ordered from current command to root
command = command.parent;
}
groupListeners.slice().reverse().forEach(command => command.emit('beforeAllHelp', context));
getCommandAndParents(this).reverse().forEach(command => command.emit('beforeAllHelp', context));
this.emit('beforeHelp', context);
let helpInformation = this.helpInformation(context);
@ -1650,7 +1676,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
this.emit(this._helpLongFlag); // deprecated
this.emit('afterHelp', context);
groupListeners.forEach(command => command.emit('afterAllHelp', context));
getCommandAndParents(this).forEach(command => command.emit('afterAllHelp', context));
};
/**