Skip to content

Commit

Permalink
Wrap up reporters and preprocessors
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Aug 30, 2023
1 parent 298245e commit b7a138a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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<ReporterOptions> =>
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);
Expand All @@ -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')
Expand Down
24 changes: 24 additions & 0 deletions src/util/reporter.ts
Original file line number Diff line number Diff line change
@@ -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<ReporterOptions> =>
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);
};

0 comments on commit b7a138a

Please sign in to comment.