diff options
Diffstat (limited to 'subprojects/frontend/config/eslintReport.cjs')
-rw-r--r-- | subprojects/frontend/config/eslintReport.cjs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/subprojects/frontend/config/eslintReport.cjs b/subprojects/frontend/config/eslintReport.cjs new file mode 100644 index 00000000..7c4b7bd6 --- /dev/null +++ b/subprojects/frontend/config/eslintReport.cjs | |||
@@ -0,0 +1,58 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | |||
7 | const { writeFile } = require('node:fs/promises'); | ||
8 | const path = require('node:path'); | ||
9 | const { Readable } = require('node:stream'); | ||
10 | const { pipeline } = require('node:stream/promises'); | ||
11 | |||
12 | const { ESLint } = require('eslint'); | ||
13 | |||
14 | const rootDir = path.join(__dirname, '..'); | ||
15 | |||
16 | /** | ||
17 | * Write ESLint report to console. | ||
18 | * | ||
19 | * @param cli {import('eslint').ESLint} The ESLint CLI. | ||
20 | * @param report {import('eslint').ESLint.LintResult[]} The ESLint report. | ||
21 | * @return {Promise<void>} A promise that resolves when the report is finished. | ||
22 | */ | ||
23 | async function reportToConsole(cli, report) { | ||
24 | const stylishFormatter = await cli.loadFormatter('stylish'); | ||
25 | const output = new Readable(); | ||
26 | output.push(await stylishFormatter.format(report)); | ||
27 | output.push(null); | ||
28 | return pipeline(output, process.stdout); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Write ESLint report to the <code>build</code> directory. | ||
33 | * | ||
34 | * @param cli {import('eslint').ESLint} The ESLint CLI. | ||
35 | * @param report {import('eslint').ESLint.LintResult[]} The ESLint report. | ||
36 | * @return {Promise<void>} A promise that resolves when the report is finished. | ||
37 | */ | ||
38 | async function reportToJson(cli, report) { | ||
39 | const jsonFormatter = await cli.loadFormatter('json'); | ||
40 | const json = await jsonFormatter.format(report); | ||
41 | const reportPath = path.join(rootDir, 'build', 'eslint.json'); | ||
42 | return writeFile(reportPath, json, 'utf-8'); | ||
43 | } | ||
44 | |||
45 | async function createReport() { | ||
46 | const cli = new ESLint({ | ||
47 | useEslintrc: true, | ||
48 | cwd: rootDir, | ||
49 | }); | ||
50 | const report = await cli.lintFiles('.'); | ||
51 | await Promise.all([reportToConsole(cli, report), reportToJson(cli, report)]); | ||
52 | |||
53 | if (report.some((entry) => entry.errorCount > 0)) { | ||
54 | process.exitCode = 1; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | createReport().catch(console.error); | ||