build: fix JS linting (#26876)

This commit is contained in:
Samuel Attard 2020-12-10 10:57:06 -08:00 committed by GitHub
parent e641a747fb
commit 51db2a6b34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 6 deletions

2
.gitignore vendored
View file

@ -55,7 +55,7 @@ electron.d.ts
spec/.hash spec/.hash
# Eslint Cache # Eslint Cache
.eslintcache .eslintcache*
# Generated native addon files # Generated native addon files
/spec-main/fixtures/native-addon/echo/build/ /spec-main/fixtures/native-addon/echo/build/

View file

@ -1,5 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
const crypto = require('crypto');
const { GitProcess } = require('dugite'); const { GitProcess } = require('dugite');
const childProcess = require('child_process'); const childProcess = require('child_process');
const { ESLint } = require('eslint'); const { ESLint } = require('eslint');
@ -99,20 +100,25 @@ const LINTERS = [{
test: filename => filename.endsWith('.js') || filename.endsWith('.ts'), test: filename => filename.endsWith('.js') || filename.endsWith('.ts'),
run: async (opts, filenames) => { run: async (opts, filenames) => {
const eslint = new ESLint({ const eslint = new ESLint({
cache: true, // Do not use the lint cache on CI builds
cache: !process.env.CI,
cacheLocation: `node_modules/.eslintcache.${crypto.createHash('md5').update(fs.readFileSync(__filename)).digest('hex')}`,
extensions: ['.js', '.ts'], extensions: ['.js', '.ts'],
fix: opts.fix fix: opts.fix
}); });
const formatter = await eslint.loadFormatter(); const formatter = await eslint.loadFormatter();
let successCount = 0; let successCount = 0;
for (const result of await eslint.lintFiles(filenames)) { const results = await eslint.lintFiles(filenames);
for (const result of results) {
successCount += result.errorCount === 0 ? 1 : 0; successCount += result.errorCount === 0 ? 1 : 0;
if (result.errorCount !== 0 || result.warningCount !== 0) { if (opts.verbose && result.errorCount === 0 && result.warningCount === 0) {
console.log(formatter.format([result]));
} else if (opts.verbose) {
console.log(`${result.filePath}: no errors or warnings`); console.log(`${result.filePath}: no errors or warnings`);
} }
} }
console.log(formatter.format(results));
if (opts.fix) {
await ESLint.outputFixes(results);
}
if (successCount !== filenames.length) { if (successCount !== filenames.length) {
console.error('Linting had errors'); console.error('Linting had errors');
process.exit(1); process.exit(1);