Skip to content

Commit

Permalink
chore: Applied changes to program.js for compatibility with yargs v17
Browse files Browse the repository at this point in the history
  • Loading branch information
rpl committed Jun 7, 2022
1 parent 7720728 commit 5d19c65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
30 changes: 21 additions & 9 deletions src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class Program {
verboseEnabled: boolean;
options: Object;
programArgv: Array<string>;
demandedOptions: Object;

constructor(
argv: ?Array<string>,
Expand Down Expand Up @@ -168,9 +169,17 @@ export class Program {
// regressions are located at: tests/functional/test.cli.sign.js
//
// Replace hack if possible: https://github.com/mozilla/web-ext/issues/1930
const validationInstance = this.yargs.getValidationInstance();
const validationInstance =
this.yargs.getInternalMethods().getValidationInstance();
const { requiredArguments } = validationInstance;
validationInstance.requiredArguments = () => {};
// Initialize demandedOptions (which is going to be set to an object with one
// property for each mandatory global options, then the arrow function below
// will receive as its demandedOptions parameter a new one that also includes
// all mandatory options for the sub command selected).
this.demandedOptions = this.yargs.getDemandedOptions();
validationInstance.requiredArguments = (args, demandedOptions) => {
this.demandedOptions = demandedOptions;
};
const argv = this.yargs.argv;
validationInstance.requiredArguments = requiredArguments;

Expand Down Expand Up @@ -211,8 +220,9 @@ export class Program {
// must call checkRequiredArguments() to ensure that required parameters are
// defined (in the CLI or in a config file).
checkRequiredArguments(adjustedArgv: Object): void {
const validationInstance = this.yargs.getValidationInstance();
validationInstance.requiredArguments(adjustedArgv);
const validationInstance =
this.yargs.getInternalMethods().getValidationInstance();
validationInstance.requiredArguments(adjustedArgv, this.demandedOptions);
}

// Remove WEB_EXT_* environment vars that are not a global cli options
Expand Down Expand Up @@ -430,7 +440,7 @@ Example: $0 --help run.
default: process.cwd(),
requiresArg: true,
type: 'string',
coerce: path.resolve,
coerce: (arg) => arg != null ? path.resolve(arg) : undefined,
},
'artifacts-dir': {
alias: 'a',
Expand Down Expand Up @@ -505,9 +515,10 @@ Example: $0 --help run.
demandOption: false,
requiresArg: true,
type: 'string',
coerce: throwUsageErrorIfArray(
'Multiple --filename/-n option are not allowed'
),
coerce: (arg) => arg == null ?
undefined : throwUsageErrorIfArray(
'Multiple --filename/-n option are not allowed'
)(arg),
},
'overwrite-dest': {
alias: 'o',
Expand Down Expand Up @@ -650,7 +661,8 @@ Example: $0 --help run.
demandOption: false,
requiresArg: true,
type: 'array',
coerce: coerceCLICustomPreference,
coerce: (arg) => arg != null ?
coerceCLICustomPreference(arg) : undefined,
},
'start-url': {
alias: ['u', 'url'],
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/test.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ describe('config', () => {
});

it('coerces config option values if needed', () => {
const coerce = (sourceDir) => `coerced(${sourceDir})`;
const coerce = (sourceDir) => {
// coerce may have been called with the default value (undefined)
// and in that case we want to return undefined to allow the config file
// to override the empty default value.
return sourceDir != null ? `coerced(${sourceDir})` : undefined;
};
const params = makeArgv({
userCmd: ['fakecommand'],
globalOpt: {
Expand All @@ -171,7 +176,7 @@ describe('config', () => {
type: 'string',
demandOption: false,
// In the real world this would do something like
// (sourceDir) => path.resolve(sourceDir)
// (sourceDir) => sourceDir != null ? path.resolve(sourceDir) : undefined;
coerce,
},
},
Expand Down

0 comments on commit 5d19c65

Please sign in to comment.