2020-11-07 01:11:09 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2023-08-06 21:26:04 -04:00
|
|
|
const {ESLint} = require('eslint');
|
2023-06-08 02:21:40 -04:00
|
|
|
|
|
|
|
console.log(`Running ESLint version ${ESLint.version}...`);
|
|
|
|
|
2020-11-07 01:11:09 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
2023-07-08 07:06:05 -04:00
|
|
|
function hasOption(...names) {
|
|
|
|
return process.argv.some(arg => names.includes(arg));
|
|
|
|
}
|
|
|
|
|
2020-11-07 01:11:09 -05:00
|
|
|
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, '..'));
|
|
|
|
|
2023-07-13 16:02:35 -04:00
|
|
|
const sources = ['js', 'subprojects/extensions-app/js', 'tests'];
|
2023-08-06 21:26:04 -04:00
|
|
|
const eslint = new ESLint({cache: true});
|
2020-11-07 01:11:09 -05:00
|
|
|
|
2023-08-06 21:26:04 -04:00
|
|
|
const results = await eslint.lintFiles(sources);
|
|
|
|
const formatter = await eslint.loadFormatter(getOption('--format', '-f'));
|
|
|
|
const resultText = formatter.format(results);
|
2020-11-07 01:11:09 -05:00
|
|
|
|
|
|
|
if (outputPath) {
|
|
|
|
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
|
|
|
|
fs.writeFileSync(outputPath, resultText);
|
2023-07-08 07:06:05 -04:00
|
|
|
|
|
|
|
if (hasOption('--stdout')) {
|
2023-08-06 21:26:04 -04:00
|
|
|
const consoleFormatter = await eslint.loadFormatter();
|
|
|
|
console.log(consoleFormatter.format(results));
|
2023-07-08 07:06:05 -04:00
|
|
|
}
|
2020-11-07 01:11:09 -05:00
|
|
|
} else {
|
|
|
|
console.log(resultText);
|
|
|
|
}
|
|
|
|
|
2023-08-06 21:26:04 -04:00
|
|
|
process.exitCode = results.some(r => r.errorCount > 0) ? 1 : 0;
|
2020-11-07 01:11:09 -05:00
|
|
|
})().catch((error) => {
|
|
|
|
process.exitCode = 1;
|
|
|
|
console.error(error);
|
|
|
|
});
|