Skip to content

Commit

Permalink
fix: Add --no-exit-code option to prevent spelling issues from impa…
Browse files Browse the repository at this point in the history
…cting the exit code. (#4809)

Co-authored-by: Kevin Balke <[email protected]>
Co-authored-by: Jason Dent <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent 611e0a8 commit 6df63fb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/cspell/src/app/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ exports[`Validate cli > app 'check help' Expect Error: 'outputHelp' 1`] = `
" --no-validate-directives Do not validate in-document CSpell directives.",
" --no-color Turn off color.",
" --color Force color",
" --no-exit-code Do not return an exit code if issues are found.",
" --no-default-configuration Do not load the default configuration and",
" dictionaries.",
" -h, --help display help for command",
Expand Down Expand Up @@ -513,6 +514,7 @@ exports[`Validate cli > app 'no-args' Expect Error: 'outputHelp' 1`] = `
" --no-progress Turn off progress messages",
" --no-summary Turn off summary message in console.",
" -s, --silent Silent mode, suppress error messages.",
" --no-exit-code Do not return an exit code if issues are found.",
" --quiet Only show spelling issues or errors.",
" --fail-fast Exit after first file with an issue or error.",
" -r, --root <root folder> Root directory, defaults to current directory.",
Expand Down
5 changes: 4 additions & 1 deletion packages/cspell/src/app/commandCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function commandCheck(prog: Command): Command {
.option('--no-validate-directives', 'Do not validate in-document CSpell directives.')
.option('--no-color', 'Turn off color.')
.option('--color', 'Force color')
.option('--no-exit-code', 'Do not return an exit code if issues are found.')
.addOption(
new CommanderOption(
'--default-configuration',
Expand All @@ -34,6 +35,7 @@ export function commandCheck(prog: Command): Command {
),
)
.action(async (files: string[], options: CheckCommandOptions) => {
const useExitCode = options.exitCode ?? true;
App.parseApplicationFeatureFlags(options.flag);
let issueCount = 0;
for (const filename of files) {
Expand All @@ -60,7 +62,8 @@ export function commandCheck(prog: Command): Command {
console.log();
}
if (issueCount) {
throw new CheckFailed('Issues found', 1);
const exitCode = useExitCode ?? true ? 1 : 0;
throw new CheckFailed('Issues found', exitCode);
}
});
}
8 changes: 7 additions & 1 deletion packages/cspell/src/app/commandLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function commandLint(prog: Command): Command {
.option('--no-progress', 'Turn off progress messages')
.option('--no-summary', 'Turn off summary message in console.')
.option('-s, --silent', 'Silent mode, suppress error messages.')
.option('--no-exit-code', 'Do not return an exit code if issues are found.')
.addOption(
new CommanderOption('--quiet', 'Only show spelling issues or errors.').implies({
summary: false,
Expand Down Expand Up @@ -162,16 +163,21 @@ export function commandLint(prog: Command): Command {
.addHelpText('after', advanced)
.arguments('[globs...]')
.action((fileGlobs: string[], options: LinterCliOptions) => {
const useExitCode = options.exitCode ?? true;
App.parseApplicationFeatureFlags(options.flag);
const { mustFindFiles, fileList } = options;
return App.lint(fileGlobs, options).then((result) => {
if (!fileGlobs.length && !result.files && !result.errors && !fileList) {
spellCheckCommand.outputHelp();
throw new CheckFailed('outputHelp', 1);
}
if (result.issues || result.errors || (mustFindFiles && !result.files)) {
if (result.errors || (mustFindFiles && !result.files)) {
throw new CheckFailed('check failed', 1);
}
if (result.issues) {
const exitCode = useExitCode ? 1 : 0;
throw new CheckFailed('check failed', exitCode);
}
return;
});
});
Expand Down
6 changes: 6 additions & 0 deletions packages/cspell/src/app/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ export interface BaseOptions {
*/
validateDirectives?: boolean;

/**
* Return an exit code if there are issues found.
* @default true
*/
exitCode?: boolean;

/**
* Execution flags.
* Used primarily for releasing experimental features.
Expand Down

0 comments on commit 6df63fb

Please sign in to comment.