|
| 1 | +// Minimal ESLint multiple formatter for Nx |
| 2 | +const { writeFileSync } = require('fs'); |
| 3 | +const { dirname, resolve } = require('path'); |
| 4 | +const { sync: mkdirp } = require('mkdirp'); |
| 5 | +const { ESLint } = require('eslint'); |
| 6 | + |
| 7 | +const eslint = new ESLint(); |
| 8 | + |
| 9 | +module.exports = async function (results, context) { |
| 10 | + // Default configuration |
| 11 | + let config = { |
| 12 | + formatters: [ |
| 13 | + { |
| 14 | + name: 'stylish', |
| 15 | + output: 'console', |
| 16 | + }, |
| 17 | + ], |
| 18 | + globalOptions: { |
| 19 | + verbose: false, |
| 20 | + timestamp: false, |
| 21 | + showProgress: false, |
| 22 | + }, |
| 23 | + }; |
| 24 | + |
| 25 | + // Parse ESLINT_EXTRA_FORMATS - support both JSON and comma-separated formats |
| 26 | + if (process.env.ESLINT_EXTRA_FORMATS) { |
| 27 | + const extraFormatsValue = process.env.ESLINT_EXTRA_FORMATS.trim(); |
| 28 | + |
| 29 | + try { |
| 30 | + // Try to parse as JSON first |
| 31 | + const jsonConfig = JSON.parse(extraFormatsValue); |
| 32 | + |
| 33 | + // Merge with default config |
| 34 | + if (jsonConfig.formatters) { |
| 35 | + // Replace default formatters with JSON config formatters |
| 36 | + config.formatters = [...jsonConfig.formatters]; |
| 37 | + } |
| 38 | + |
| 39 | + if (jsonConfig.globalOptions) { |
| 40 | + config.globalOptions = { |
| 41 | + ...config.globalOptions, |
| 42 | + ...jsonConfig.globalOptions, |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + // Handle additional JSON properties |
| 47 | + if (jsonConfig.verbose !== undefined) |
| 48 | + config.globalOptions.verbose = jsonConfig.verbose; |
| 49 | + if (jsonConfig.timestamp !== undefined) |
| 50 | + config.globalOptions.timestamp = jsonConfig.timestamp; |
| 51 | + if (jsonConfig.showProgress !== undefined) |
| 52 | + config.globalOptions.showProgress = jsonConfig.showProgress; |
| 53 | + } catch (error) { |
| 54 | + // Fallback to comma-separated format for backwards compatibility |
| 55 | + const extraFormats = extraFormatsValue.split(','); |
| 56 | + for (const format of extraFormats) { |
| 57 | + const trimmedFormat = format.trim(); |
| 58 | + if ( |
| 59 | + trimmedFormat && |
| 60 | + !config.formatters.some(f => f.name === trimmedFormat) |
| 61 | + ) { |
| 62 | + config.formatters.push({ |
| 63 | + name: trimmedFormat, |
| 64 | + output: 'console', |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + // Always ensure JSON formatter for Nx (unless already configured) |
| 72 | + if (!config.formatters.some(f => f.name === 'json')) { |
| 73 | + config.formatters.push({ |
| 74 | + name: 'json', |
| 75 | + output: 'file', |
| 76 | + path: 'eslint-report.json', |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + // Apply global options |
| 81 | + if (config.globalOptions.timestamp) { |
| 82 | + console.log(`ESLint run at: ${new Date().toISOString()}`); |
| 83 | + } |
| 84 | + |
| 85 | + let jsonResult = ''; |
| 86 | + |
| 87 | + for (const formatterConfig of config.formatters) { |
| 88 | + if (config.globalOptions.verbose) { |
| 89 | + console.log(`Using formatter: ${formatterConfig.name}`); |
| 90 | + } |
| 91 | + |
| 92 | + const formatter = await eslint.loadFormatter(formatterConfig.name); |
| 93 | + let formatterResult = formatter.format(results); |
| 94 | + |
| 95 | + // Apply formatter-specific options |
| 96 | + if ( |
| 97 | + formatterConfig.options && |
| 98 | + formatterConfig.name === 'json' && |
| 99 | + formatterConfig.options.pretty |
| 100 | + ) { |
| 101 | + try { |
| 102 | + const parsed = JSON.parse(formatterResult); |
| 103 | + formatterResult = JSON.stringify(parsed, null, 2); |
| 104 | + } catch (e) { |
| 105 | + // Keep original if parsing fails |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (formatterConfig.output === 'console') { |
| 110 | + console.log(formatterResult); |
| 111 | + } else if (formatterConfig.output === 'file') { |
| 112 | + const filePath = resolve(process.cwd(), formatterConfig.path); |
| 113 | + try { |
| 114 | + mkdirp(dirname(filePath)); |
| 115 | + writeFileSync(filePath, formatterResult); |
| 116 | + |
| 117 | + if (config.globalOptions.verbose) { |
| 118 | + console.log(`Written ${formatterConfig.name} output to: ${filePath}`); |
| 119 | + } |
| 120 | + } catch (ex) { |
| 121 | + console.error('Error writing output file:', ex.message); |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Store JSON result for Nx output file handling |
| 127 | + if (formatterConfig.name === 'json') { |
| 128 | + jsonResult = formatterResult; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + // Return JSON result for Nx to write to outputFile |
| 133 | + return jsonResult || JSON.stringify(results); |
| 134 | +}; |
0 commit comments