1 | import chalk from 'chalk';
|
2 | import { Option as CommanderOption } from 'commander';
|
3 | import * as App from './application.mjs';
|
4 | import { checkText } from './application.mjs';
|
5 | import { console } from './console.js';
|
6 | import { CheckFailed } from './util/errors.js';
|
7 | export 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 |
|
\ | No newline at end of file |