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

fix(cli): Fix global flag parsing interference #235

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.22.1

- fix(cli): Fix global flag parsing interference (#235)

## 0.22.0

- feat(config): Automatically detect GitHub config when missing (#208)
Expand Down
58 changes: 44 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ function printVersion(): void {
}
}

const GLOBAL_BOOLEAN_FLAGS = {
'no-input': {
coerce: envToBool,
default: isCI,
describe: 'Suppresses all user prompts',
global: true,
},
'dry-run': {
coerce: envToBool,
// TODO(byk): Deprecate this in favor of CRAFT_DRY_RUN
default: process.env.DRY_RUN,
global: true,
describe: 'Dry run mode: do not perform any real actions',
},
};

/**
* This function is to pre-process and fix one of yargs' shortcomings:
* We want to use some flags as booleans: just their existence on the CLI should
* set them to true. That said since we also allow setting them through
* environment variables, we need to parse many string values that would set
* them to false. Moreover, we want to be able to override already-set env
* variables with the `--flag=no` kind of notation (using the `=` symbol) but
* not via the positional argument notation (`--flag no`). The only way to do
chadwhitacre marked this conversation as resolved.
Show resolved Hide resolved
* this is to define them as string arguments and then _inject_ a truthy string
* if we notice the flag is passed standalone (ie `--flag`).
* @param argv The raw process.argv array
* @returns The processed, injected version of the argv array to pass to yargs
*/
function fixGlobalBooleanFlags(argv: string[]): string[] {
const result = [];
for (const arg of argv) {
result.push(arg);
if (arg.slice(2) in GLOBAL_BOOLEAN_FLAGS) {
result.push('1');
}
}
return result;
}

/**
* Main entrypoint
*/
Expand All @@ -32,6 +72,8 @@ function main(): void {

initSentrySdk();

const argv = fixGlobalBooleanFlags(process.argv.slice(2));

yargs
.parserConfiguration({
'boolean-negation': false,
Expand All @@ -47,19 +89,7 @@ function main(): void {
.alias('v', 'version')
.help()
.alias('h', 'help')
.option('no-input', {
coerce: envToBool,
default: isCI,
describe: 'Suppresses all user prompts',
global: true,
})
.option('dry-run', {
coerce: envToBool,
// TODO(byk): Deprecate this in favor of CRAFT_DRY_RUN
default: process.env.DRY_RUN,
global: true,
describe: 'Dry run mode: do not perform any real actions',
})
.options(GLOBAL_BOOLEAN_FLAGS)
.option('log-level', {
default: 'Info',
choices: Object.keys(LogLevel).filter(level => isNaN(Number(level))),
Expand All @@ -70,7 +100,7 @@ function main(): void {
.strictCommands()
.showHelpOnFail(true)
.middleware(setGlobals)
.parse();
.parse(argv);
}

main();