Files
HiveGrid-Wireframes/hivegrid-cloud/scripts/eslint-staged.mjs
T

49 lines
1.4 KiB
JavaScript

#!/usr/bin/env node
// Wrapper invoked by the lefthook pre-commit `lint-staged` step. ESLint v8
// raises a "File ignored because of a matching ignore pattern" warning when an
// explicitly-passed path matches an ignorePatterns entry — which together with
// --max-warnings=0 makes every commit touching `apps/web/app/**` fail, since
// the root .eslintrc ignores `app/` by default. v9's --no-warn-ignored would
// fix this directly; until that upgrade lands we filter the file list against
// the ESLint API and only lint the files ESLint would actually inspect.
import { ESLint } from "eslint";
const files = process.argv.slice(2);
if (files.length === 0) {
process.exit(0);
}
const eslint = new ESLint({ errorOnUnmatchedPattern: false });
const candidates = [];
for (const file of files) {
if (!(await eslint.isPathIgnored(file))) {
candidates.push(file);
}
}
if (candidates.length === 0) {
process.exit(0);
}
const results = await eslint.lintFiles(candidates);
const formatter = await eslint.loadFormatter("stylish");
const output = await formatter.format(results);
if (output) {
process.stdout.write(output);
if (!output.endsWith("\n")) process.stdout.write("\n");
}
let errorCount = 0;
let warningCount = 0;
for (const result of results) {
errorCount += result.errorCount;
warningCount += result.warningCount;
}
if (errorCount > 0 || warningCount > 0) {
process.exit(1);
}
process.exit(0);