From 93657252466f6428f09aa389f7b97586fdb0e092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=BCllner?= Date: Sun, 9 Jul 2023 15:34:54 +0200 Subject: [PATCH] ci: Use wrapper to run eslint The eslint job report its results as artifacts in junit format, so that gitlab can present them in its UI. However many psople miss that, and unsuccessfully check the logs instead. Address this by using a simplified version of gnome-shell's eslint wrapper, so we can report results both on stdout and in a file without re-running the linter. Part-of: --- .gitlab-ci.yml | 3 ++- .gitlab-ci/run-eslint | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100755 .gitlab-ci/run-eslint diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b1562b3b..5981e068 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -107,7 +107,8 @@ eslint: stage: review <<: *prereview_req script: - - eslint -o $LINT_LOG -f junit --resolve-plugins-relative-to $(npm root -g) extensions + - export NODE_PATH=$(npm root -g) + - ./.gitlab-ci/run-eslint --output-file ${LINT_LOG} --format junit --stdout artifacts: paths: - ${LINT_LOG} diff --git a/.gitlab-ci/run-eslint b/.gitlab-ci/run-eslint new file mode 100755 index 00000000..ac0b281a --- /dev/null +++ b/.gitlab-ci/run-eslint @@ -0,0 +1,54 @@ +#!/usr/bin/env node + +const {ESLint} = require('eslint'); + +console.log(`Running ESLint version ${ESLint.version}...`); + +const fs = require('fs'); +const path = require('path'); + +function hasOption(...names) { + return process.argv.some(arg => names.includes(arg)); +} + +function getOption(...names) { + const optIndex = + process.argv.findIndex(arg => names.includes(arg)) + 1; + + if (optIndex === 0) + return undefined; + + return process.argv[optIndex]; +} + +(async function main() { + const outputOption = getOption('--output-file', '-o'); + const outputPath = outputOption ? path.resolve(outputOption) : null; + + const sourceDir = path.dirname(process.argv[1]); + process.chdir(path.resolve(sourceDir, '..')); + + const sources = ['extensions']; + const eslint = new ESLint(); + + const results = await eslint.lintFiles(sources); + const formatter = await eslint.loadFormatter(getOption('--format', '-f')); + const resultText = formatter.format(results); + + if (outputPath) { + fs.mkdirSync(path.dirname(outputPath), {recursive: true}); + fs.writeFileSync(outputPath, resultText); + + if (hasOption('--stdout')) { + const consoleFormatter = await eslint.loadFormatter(); + console.log(consoleFormatter.format(results)); + } + } else { + console.log(resultText); + } + + process.exitCode = results.some(r => r.errorCount > 0) ? 1 : 0; +})().catch((error) => { + process.exitCode = 1; + console.error(error); +});