aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/config/eslintReport.cjs
blob: 7c4b7bd673576ab253b6a65016462d7451231c41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

const { writeFile } = require('node:fs/promises');
const path = require('node:path');
const { Readable } = require('node:stream');
const { pipeline } = require('node:stream/promises');

const { ESLint } = require('eslint');

const rootDir = path.join(__dirname, '..');

/**
 * Write ESLint report to console.
 *
 * @param cli {import('eslint').ESLint} The ESLint CLI.
 * @param report {import('eslint').ESLint.LintResult[]} The ESLint report.
 * @return {Promise<void>} A promise that resolves when the report is finished.
 */
async function reportToConsole(cli, report) {
  const stylishFormatter = await cli.loadFormatter('stylish');
  const output = new Readable();
  output.push(await stylishFormatter.format(report));
  output.push(null);
  return pipeline(output, process.stdout);
}

/**
 * Write ESLint report to the <code>build</code> directory.
 *
 * @param cli {import('eslint').ESLint} The ESLint CLI.
 * @param report {import('eslint').ESLint.LintResult[]} The ESLint report.
 * @return {Promise<void>} A promise that resolves when the report is finished.
 */
async function reportToJson(cli, report) {
  const jsonFormatter = await cli.loadFormatter('json');
  const json = await jsonFormatter.format(report);
  const reportPath = path.join(rootDir, 'build', 'eslint.json');
  return writeFile(reportPath, json, 'utf-8');
}

async function createReport() {
  const cli = new ESLint({
    useEslintrc: true,
    cwd: rootDir,
  });
  const report = await cli.lintFiles('.');
  await Promise.all([reportToConsole(cli, report), reportToJson(cli, report)]);

  if (report.some((entry) => entry.errorCount > 0)) {
    process.exitCode = 1;
  }
}

createReport().catch(console.error);