const path = require('path');
const color = require('colors');
const program = require('commander');
+const worker = require('worker_threads');
const eslint = require('pve-eslint');
program
.option('-e, --extend <configfile>', 'uses <configfile> ontop of default eslint config.')
.option('-f, --fix', 'if set, fixes will be applied.')
.option('-s, --strict', 'if set, also exit uncleanly on warnings')
+ .option('-t, --threads <threads>', 'how many worker_threads should be used (default=4)')
.option('--output-config', 'if set, only output the config as JSON and exit.')
;
paths = [process.cwd()];
}
+let threadCount = 4;
+if (program.threads) {
+ threadCount = program.threads;
+}
+
const defaultConfig = {
parserOptions: {
ecmaVersion: 2020,
process.exit(0);
}
-const cli = new eslint.CLIEngine({
+const cliOptions = {
baseConfig: config,
useEslintrc: true,
fix: !!program.fix,
cwd: process.cwd(),
-});
+};
+
+let promises = [];
+let filesPerThread = Math.round(paths.length / threadCount);
+for (let i = 0; i < (threadCount - 1); i++) {
+ let files = paths.splice(0, filesPerThread);
+ promises.push(eslint.createWorker({
+ cliOptions,
+ files
+ }));
+}
+
+// the remaining paths
+promises.push(eslint.createWorker({
+ cliOptions,
+ files: paths
+}));
-const report = cli.executeOnFiles(paths);
+let results = (await Promise.all(promises)).map(res => res.results).flat(1);
let exitcode = 0;
let files_err = [], files_warn = [], files_ok = [];
let fixes = 0;
console.log('------------------------------------------------------------');
-report.results.forEach(function(result) {
+results.forEach(function(result) {
let filename = path.relative(process.cwd(), result.filePath);
let msgs = result.messages;
let max_sev = 0;
console.log('------------------------------------------------------------');
});
-if (report.results.length > 1) {
+if (results.length > 1) {
console.log(`${color.bold(files_ok.length + files_err.length)} files:`);
if (files_err.length > 0) {
console.log(color.red(` ${color.bold(files_err.length)} files have Errors`));
if (program.fix) {
if (fixes > 0) {
console.log(`Writing ${color.bold(fixes)} fixed files...`);
- eslint.CLIEngine.outputFixes(report);
+ eslint.CLIEngine.outputFixes({ results });
console.log('Done');
} else {
console.log("No fixable Errors/Warnings found.");
--- /dev/null
+'use strict';
+
+const worker = require('worker_threads');
+
+if (!worker.isMainThread) {
+ const eslint = require('pve-eslint');
+ const data = worker.workerData;
+ const cli = new eslint.CLIEngine(data.cliOptions);
+ const report = cli.executeOnFiles(data.files);
+ worker.parentPort.postMessage(report);
+} else {
+ module.exports = async function createWorker(workerData) {
+ return new Promise((resolve, reject) => {
+ const child = new worker.Worker(__filename,
+ {
+ workerData,
+ },
+ );
+ child.on('message', resolve);
+ child.on('error', reject);
+ child.on('exit', (code) => {
+ if (code !== 0) {reject(new Error(`Worker stopped with exit code ${code}`));}
+ });
+ });
+ }
+}
+