aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/eslintReport.cjs
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/frontend/config/eslintReport.cjs')
-rw-r--r--subprojects/frontend/config/eslintReport.cjs58
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
7const { writeFile } = require('node:fs/promises');
8const path = require('node:path');
9const { Readable } = require('node:stream');
10const { pipeline } = require('node:stream/promises');
11
12const { ESLint } = require('eslint');
13
14const 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 */
23async 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 */
38async 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
45async 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
58createReport().catch(console.error);