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