-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmultiple-formats.ts
More file actions
83 lines (77 loc) · 2.5 KB
/
multiple-formats.ts
File metadata and controls
83 lines (77 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { ESLint } from 'eslint';
import * as process from 'node:process';
import type { FormatterConfig } from './types.js';
import {
type EslintFormat,
formatTerminalOutput,
findConfigFromEnv as getConfigFromEnv,
persistEslintReports,
} from './utils.js';
export const DEFAULT_OUTPUT_DIR = '.eslint';
export const DEFAULT_FILENAME = 'eslint-report';
export const DEFAULT_FORMATS = ['json'] as EslintFormat[];
export const DEFAULT_TERMINAL = 'stylish' as EslintFormat;
export const DEFAULT_CONFIG: Required<
Pick<
FormatterConfig,
'outputDir' | 'filename' | 'formats' | 'terminal' | 'verbose'
>
> = {
outputDir: DEFAULT_OUTPUT_DIR,
filename: DEFAULT_FILENAME,
formats: DEFAULT_FORMATS,
terminal: DEFAULT_TERMINAL,
verbose: false,
};
/**
* Format ESLint results using multiple configurable formatters
*
* @param results - The ESLint results
* @param args - The arguments passed to the formatter
* @returns The formatted results for terminal display
*
* @example
* // Basic usage:
* ESLINT_FORMATTER_CONFIG='{"filename":"lint-results","formats":["json"],"terminal":"stylish"}' npx eslint .
* // Creates: .eslint/eslint-results.json + terminal output
*
* // With custom output directory:
* ESLINT_FORMATTER_CONFIG='{"outputDir":"./ci-reports","filename":"eslint-report","formats":["json","html"],"terminal":"stylish"}' nx lint utils
* // Creates: ci-reports/eslint-report.json, ci-reports/eslint-report.html + terminal output
*
* Configuration schema:
* {
* "outputDir": "./reports", // Optional: Output directory (default: cwd/.eslint)
* "filename": "eslint-report", // Optional: Base filename without extension (default: 'eslint-report')
* "formats": ["json"], // Optional: Array of format names for file output (default: ['json'])
* "terminal": "stylish" // Optional: Format for terminal output (default: 'stylish')
* }
*/
export default async function multipleFormats(
results: ESLint.LintResult[],
_args?: unknown,
): Promise<string> {
const config = {
...DEFAULT_CONFIG,
...getConfigFromEnv(process.env),
} satisfies FormatterConfig;
const {
outputDir = DEFAULT_OUTPUT_DIR,
filename,
formats,
terminal,
verbose = false,
} = config;
try {
await persistEslintReports(formats, results, {
outputDir,
filename,
verbose,
});
} catch (error) {
if (verbose) {
console.error('Error writing ESLint reports:', error);
}
}
return formatTerminalOutput(terminal, results);
}