1 | import { opMap, opTap, pipeAsync, toAsyncIterable } from '@cspell/cspell-pipe';
|
2 | import { checkTextDocument, getDefaultSettings, getGlobalSettingsAsync, mergeSettings, SuggestionError, suggestionsForWords, traceWordsAsync, } from 'cspell-lib';
|
3 | import { getReporter } from './cli-reporter.js';
|
4 | import { console } from './console.js';
|
5 | import { getFeatureFlags, parseFeatureFlags } from './featureFlags/index.js';
|
6 | import { LintRequest, runLint } from './lint/index.js';
|
7 | import { fixLegacy } from './options.js';
|
8 | import { simpleRepl } from './repl/index.js';
|
9 | import { fileInfoToDocument, readConfig, readFileInfo } from './util/fileHelper.js';
|
10 | import { finalizeReporter } from './util/reporters.js';
|
11 | import { readStdin } from './util/stdin.js';
|
12 | import { getTimeMeasurer } from './util/timer.js';
|
13 | import * as util from './util/util.js';
|
14 | export { IncludeExcludeFlag } from 'cspell-lib';
|
15 | export function lint(fileGlobs, options, reporter) {
|
16 | options = fixLegacy(options);
|
17 | const reporterOptions = { ...options, console };
|
18 | const cfg = new LintRequest(fileGlobs, options, finalizeReporter(reporter) ?? getReporter({ ...options, fileGlobs }, reporterOptions));
|
19 | return runLint(cfg);
|
20 | }
|
21 | export async function* trace(words, options) {
|
22 | options = fixLegacy(options);
|
23 | const iWords = options.stdin ? toAsyncIterable(words, readStdin()) : words;
|
24 | const { languageId, locale, allowCompoundWords, ignoreCase } = options;
|
25 | const configFile = await readConfig(options.config, undefined);
|
26 | const loadDefault = options.defaultConfiguration ?? configFile.config.loadDefaultConfiguration ?? true;
|
27 | const config = mergeSettings(await getDefaultSettings(loadDefault), await getGlobalSettingsAsync(), configFile.config);
|
28 | yield* traceWordsAsync(iWords, config, util.clean({ languageId, locale, ignoreCase, allowCompoundWords }));
|
29 | }
|
30 | export async function checkText(filename, options) {
|
31 | options = fixLegacy(options);
|
32 | const fileInfo = await readFileInfo(filename);
|
33 | const { locale, languageId, validateDirectives } = options;
|
34 | const doc = fileInfoToDocument(fileInfo, languageId, locale);
|
35 | const checkOptions = {
|
36 | configFile: options.config,
|
37 | validateDirectives,
|
38 | };
|
39 | const settingsFromCommandLine = util.clean({
|
40 | languageId,
|
41 | language: locale,
|
42 | loadDefaultConfiguration: options.defaultConfiguration,
|
43 | });
|
44 | return checkTextDocument(doc, util.clean({ ...checkOptions }), settingsFromCommandLine);
|
45 | }
|
46 | export async function* suggestions(words, options) {
|
47 | options = fixLegacy(options);
|
48 | const configFile = await readConfig(options.config, undefined);
|
49 | let timer;
|
50 | function tapStart() {
|
51 | timer = getTimeMeasurer();
|
52 | }
|
53 | function mapStart(v) {
|
54 | tapStart();
|
55 | return v;
|
56 | }
|
57 | function mapEnd(v) {
|
58 | const elapsedTimeMs = timer?.();
|
59 | return elapsedTimeMs ? { ...v, elapsedTimeMs } : v;
|
60 | }
|
61 | const iWords = options.repl
|
62 | ? pipeAsync(toAsyncIterable(words, simpleRepl()), opTap(tapStart))
|
63 | : options.useStdin
|
64 | ? pipeAsync(toAsyncIterable(words, readStdin()), opTap(tapStart))
|
65 | : words.map(mapStart);
|
66 | try {
|
67 | const results = pipeAsync(suggestionsForWords(iWords, util.clean({ ...options }), configFile.config), opMap(mapEnd));
|
68 | yield* results;
|
69 | }
|
70 | catch (e) {
|
71 | if (!(e instanceof SuggestionError))
|
72 | throw e;
|
73 | console.error(e.message);
|
74 | process.exitCode = 1;
|
75 | }
|
76 | }
|
77 | export function createInit() {
|
78 | return Promise.reject();
|
79 | }
|
80 | function registerApplicationFeatureFlags() {
|
81 | const ff = getFeatureFlags();
|
82 | const flags = [{ name: 'timer', description: 'Display elapsed time for command.' }];
|
83 | flags.forEach((flag) => ff.register(flag));
|
84 | return ff;
|
85 | }
|
86 | export function parseApplicationFeatureFlags(flags) {
|
87 | const ff = registerApplicationFeatureFlags();
|
88 | return parseFeatureFlags(flags, ff);
|
89 | }
|
90 |
|
\ | No newline at end of file |