diff options
author | Kristóf Marussy <kristof@marussy.com> | 2024-04-07 01:59:09 +0200 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2024-04-07 01:59:09 +0200 |
commit | 1d2b5c85a177b4be0cc270b8e6e54488e23da5a1 (patch) | |
tree | c8b87c875faa7e6048e5ba41b5a9e4449263fc6a /scripts | |
parent | chore(deps): upgrade to frontend-jdk17 8.0.0 (diff) | |
download | refinery-1d2b5c85a177b4be0cc270b8e6e54488e23da5a1.tar.gz refinery-1d2b5c85a177b4be0cc270b8e6e54488e23da5a1.tar.zst refinery-1d2b5c85a177b4be0cc270b8e6e54488e23da5a1.zip |
build: move Javascript config to top level
We need this to support multiple Javascript subprojects (e.g., a frontend and a
documentation website).
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/eslintReport.cjs | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/scripts/eslintReport.cjs b/scripts/eslintReport.cjs new file mode 100644 index 00000000..647a4253 --- /dev/null +++ b/scripts/eslintReport.cjs | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2024 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 | /** | ||
15 | * Write ESLint report to console. | ||
16 | * | ||
17 | * @param cli {import('eslint').ESLint} The ESLint CLI. | ||
18 | * @param report {import('eslint').ESLint.LintResult[]} The ESLint report. | ||
19 | * @return {Promise<void>} A promise that resolves when the report is finished. | ||
20 | */ | ||
21 | async function reportToConsole(cli, report) { | ||
22 | const stylishFormatter = await cli.loadFormatter('stylish'); | ||
23 | const output = new Readable(); | ||
24 | output.push(await stylishFormatter.format(report)); | ||
25 | output.push(null); | ||
26 | return pipeline(output, process.stdout); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Write ESLint report to the <code>build</code> directory. | ||
31 | * | ||
32 | * @param cli {import('eslint').ESLint} The ESLint CLI. | ||
33 | * @param workspace {string} The workspace path. | ||
34 | * @param report {import('eslint').ESLint.LintResult[]} The ESLint report. | ||
35 | * @return {Promise<void>} A promise that resolves when the report is finished. | ||
36 | */ | ||
37 | async function reportToJson(cli, workspace, report) { | ||
38 | const jsonFormatter = await cli.loadFormatter('json'); | ||
39 | const json = await jsonFormatter.format(report); | ||
40 | const reportPath = path.join(workspace, 'build', 'eslint.json'); | ||
41 | return writeFile(reportPath, json, 'utf-8'); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Write ESLint report to both the console and the <code>build</code> directory. | ||
46 | * | ||
47 | * @param workspace {string | undefined} The workspace path or `undefined` | ||
48 | * for the root workspace. | ||
49 | * @param fix {boolean} `true` if errors should be fixed. | ||
50 | * @return {Promise<void>} A promise that resolves when the report is finished. | ||
51 | */ | ||
52 | async function createReport(workspace, fix) { | ||
53 | const absoluteWorkspace = path.resolve(__dirname, '..', workspace ?? '.'); | ||
54 | /** @type {import('eslint').ESLint.Options} */ | ||
55 | const options = { | ||
56 | useEslintrc: true, | ||
57 | cwd: absoluteWorkspace, | ||
58 | fix, | ||
59 | }; | ||
60 | if (workspace === undefined) { | ||
61 | options.overrideConfig = { | ||
62 | ignorePatterns: ['subprojects/**/*'], | ||
63 | }; | ||
64 | } | ||
65 | const cli = new ESLint(options); | ||
66 | const report = await cli.lintFiles('.'); | ||
67 | await Promise.all([ | ||
68 | reportToConsole(cli, report), | ||
69 | reportToJson(cli, absoluteWorkspace, report), | ||
70 | ]); | ||
71 | |||
72 | if (report.some((entry) => entry.errorCount > 0)) { | ||
73 | process.exitCode = 1; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | const fixArg = '--fix'; | ||
78 | let fix = false; | ||
79 | /** @type {string | undefined} */ | ||
80 | let workspace; | ||
81 | if (process.argv[2] === fixArg) { | ||
82 | fix = true; | ||
83 | } else { | ||
84 | /* eslint-disable-next-line prefer-destructuring -- | ||
85 | * Destructuring is harder to read here. | ||
86 | */ | ||
87 | workspace = process.argv[2]; | ||
88 | fix = process.argv[3] === fixArg; | ||
89 | } | ||
90 | createReport(workspace, fix).catch(console.error); | ||