diff --git a/src/cli.ts b/src/cli.ts index 8c86395a1..8b4fe72cd 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -2,12 +2,11 @@ import './util/register.js'; import prettyMilliseconds from 'pretty-ms'; -import internalReporters from './reporters/index.js'; import parsedArgValues, { helpText } from './util/cli-arguments.js'; import { isKnownError, getKnownError, isConfigurationError, hasCause } from './util/errors.js'; -import { _load } from './util/loader.js'; -import { cwd, resolve } from './util/path.js'; +import { cwd } from './util/path.js'; import { Performance } from './util/Performance.js'; +import { runPreprocessors, runReporters } from './util/reporter.js'; import { version } from './version.js'; import { main } from './index.js'; import type { ReporterOptions, IssueType } from './types/issues.js'; @@ -24,8 +23,6 @@ const { 'include-entry-exports': isIncludeEntryExports = false, performance: isObservePerf = false, production: isProduction = false, - preprocessor = [], - reporter = ['symbols'], 'reporter-options': reporterOptions = '', strict: isStrict = false, tsConfig, @@ -45,19 +42,6 @@ if (isVersion) { const isShowProgress = !isDebug && isNoProgress === false && process.stdout.isTTY && typeof process.stdout.cursorTo === 'function'; -const preprocessors = await Promise.all(preprocessor.map(processor => _load(resolve(processor)))); - -const processAsync = (data: ReporterOptions, processors: typeof preprocessors): Promise => - processors.length === 0 ? Promise.resolve(data) : processAsync(processors[0](data), processors.slice(1)); - -const reporters = await Promise.all( - reporter.map(async reporter => { - return reporter in internalReporters - ? internalReporters[reporter as keyof typeof internalReporters] - : await _load(resolve(reporter)); - }) -); - const run = async () => { try { const perfObserver = new Performance(isObservePerf); @@ -84,9 +68,9 @@ const run = async () => { options: reporterOptions, }; - const finalData = await processAsync(initialData, preprocessors); + const finalData = await runPreprocessors(initialData); - for (const reporter of reporters) await reporter(finalData); + await runReporters(finalData); const totalErrorCount = (Object.keys(report) as IssueType[]) .filter(reportGroup => report[reportGroup] && rules[reportGroup] === 'error') diff --git a/src/util/reporter.ts b/src/util/reporter.ts new file mode 100644 index 000000000..fd0b27bb9 --- /dev/null +++ b/src/util/reporter.ts @@ -0,0 +1,24 @@ +import internalReporters from '../reporters/index.js'; +import parsedArgValues from '../util/cli-arguments.js'; +import { _load } from './loader.js'; +import { isInternal, resolve } from './path.js'; +import type { ReporterOptions } from '../types/issues.js'; + +const { preprocessor = [], reporter = ['symbols'] } = parsedArgValues; + +const preprocessors = await Promise.all(preprocessor.map(proc => _load(isInternal(proc) ? resolve(proc) : proc))); + +export const runPreprocessors = (data: ReporterOptions, processors = preprocessors): Promise => + processors.length === 0 ? Promise.resolve(data) : runPreprocessors(processors[0](data), processors.slice(1)); + +const reporters = await Promise.all( + reporter.map(async reporter => { + return reporter in internalReporters + ? internalReporters[reporter as keyof typeof internalReporters] + : await _load(isInternal(reporter) ? resolve(reporter) : reporter); + }) +); + +export const runReporters = async (options: ReporterOptions) => { + for (const reporter of reporters) await reporter(options); +};