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