UNPKG

2.52 kBJavaScriptView Raw
1import chalk from 'chalk';
2import { Option as CommanderOption } from 'commander';
3import * as App from './application.mjs';
4import { checkText } from './application.mjs';
5import { console } from './console.js';
6import { CheckFailed } from './util/errors.js';
7export function commandCheck(prog) {
8 return prog
9 .command('check <files...>')
10 .description('Spell check file(s) and display the result. The full file is displayed in color.')
11 .option('-c, --config <cspell.json>', 'Configuration file to use. By default cspell looks for cspell.json in the current directory.')
12 .option('--validate-directives', 'Validate in-document CSpell directives.')
13 .option('--no-validate-directives', 'Do not validate in-document CSpell directives.')
14 .option('--no-color', 'Turn off color.')
15 .option('--color', 'Force color')
16 .option('--no-exit-code', 'Do not return an exit code if issues are found.')
17 .addOption(new CommanderOption('--default-configuration', 'Load the default configuration and dictionaries.').hideHelp())
18 .addOption(new CommanderOption('--no-default-configuration', 'Do not load the default configuration and dictionaries.'))
19 .action(async (files, options) => {
20 const useExitCode = options.exitCode ?? true;
21 App.parseApplicationFeatureFlags(options.flag);
22 let issueCount = 0;
23 for (const filename of files) {
24 console.log(chalk.yellowBright(`Check file: ${filename}`));
25 console.log();
26 try {
27 const result = await checkText(filename, options);
28 for (const item of result.items) {
29 const fn = item.flagIE === App.IncludeExcludeFlag.EXCLUDE
30 ? chalk.gray
31 : item.isError
32 ? chalk.red
33 : chalk.whiteBright;
34 const t = fn(item.text);
35 process.stdout.write(t);
36 issueCount += item.isError ? 1 : 0;
37 }
38 console.log();
39 }
40 catch {
41 console.error(`File not found "${filename}"`);
42 throw new CheckFailed('File not found', 1);
43 }
44 console.log();
45 }
46 if (issueCount) {
47 const exitCode = (useExitCode ?? true) ? 1 : 0;
48 throw new CheckFailed('Issues found', exitCode);
49 }
50 });
51}
52//# sourceMappingURL=commandCheck.js.map
\No newline at end of file