2016-03-10 04:40:54 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const rulesDirs = ['tools/eslint-rules'];
|
2017-04-25 12:54:06 +02:00
|
|
|
const extensions = ['.js', '.md'];
|
2016-03-10 04:40:54 +01:00
|
|
|
// This is the maximum number of files to be linted per worker at any given time
|
2019-05-13 23:48:53 +02:00
|
|
|
const maxWorkload = 60;
|
2016-03-10 04:40:54 +01:00
|
|
|
|
|
|
|
const cluster = require('cluster');
|
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const totalCPUs = require('os').cpus().length;
|
|
|
|
|
2017-12-22 16:53:42 +01:00
|
|
|
const CLIEngine = require('eslint').CLIEngine;
|
|
|
|
const glob = require('eslint/node_modules/glob');
|
2016-03-10 04:40:54 +01:00
|
|
|
|
2016-04-30 01:47:18 +02:00
|
|
|
const cliOptions = {
|
2017-04-25 12:54:06 +02:00
|
|
|
rulePaths: rulesDirs,
|
|
|
|
extensions: extensions,
|
2016-04-30 01:47:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Check if we should fix errors that are fixable
|
|
|
|
if (process.argv.indexOf('-F') !== -1)
|
|
|
|
cliOptions.fix = true;
|
|
|
|
|
|
|
|
const cli = new CLIEngine(cliOptions);
|
2016-03-10 04:40:54 +01:00
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
2019-03-02 15:07:03 +01:00
|
|
|
let numCPUs = 1;
|
2016-03-10 04:40:54 +01:00
|
|
|
const paths = [];
|
2019-03-02 15:07:03 +01:00
|
|
|
let files = null;
|
|
|
|
let totalPaths = 0;
|
|
|
|
let failures = 0;
|
|
|
|
let successes = 0;
|
|
|
|
let lastLineLen = 0;
|
|
|
|
let curPath = 'Starting ...';
|
|
|
|
let showProgress = true;
|
2016-03-10 04:40:54 +01:00
|
|
|
const globOptions = {
|
2019-05-13 23:47:41 +02:00
|
|
|
nodir: true,
|
|
|
|
ignore: '**/node_modules/**/*'
|
2016-03-10 04:40:54 +01:00
|
|
|
};
|
|
|
|
const workerConfig = {};
|
2019-03-02 15:07:03 +01:00
|
|
|
let startTime;
|
|
|
|
let formatter;
|
|
|
|
let outFn;
|
|
|
|
let fd;
|
|
|
|
let i;
|
2016-03-10 04:40:54 +01:00
|
|
|
|
|
|
|
// Check if spreading work among all cores/cpus
|
|
|
|
if (process.argv.indexOf('-J') !== -1)
|
|
|
|
numCPUs = totalCPUs;
|
|
|
|
|
|
|
|
// Check if spreading work among an explicit number of cores/cpus
|
|
|
|
i = process.argv.indexOf('-j');
|
|
|
|
if (i !== -1) {
|
|
|
|
if (!process.argv[i + 1])
|
|
|
|
throw new Error('Missing parallel job count');
|
|
|
|
numCPUs = parseInt(process.argv[i + 1], 10);
|
|
|
|
if (!isFinite(numCPUs) || numCPUs <= 0)
|
|
|
|
throw new Error('Bad parallel job count');
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:44:58 +02:00
|
|
|
// Check for custom ESLint report formatter
|
2016-03-10 04:40:54 +01:00
|
|
|
i = process.argv.indexOf('-f');
|
|
|
|
if (i !== -1) {
|
|
|
|
if (!process.argv[i + 1])
|
|
|
|
throw new Error('Missing format name');
|
|
|
|
const format = process.argv[i + 1];
|
|
|
|
formatter = cli.getFormatter(format);
|
|
|
|
if (!formatter)
|
|
|
|
throw new Error('Invalid format name');
|
|
|
|
// Automatically disable progress display
|
|
|
|
showProgress = false;
|
|
|
|
// Tell worker to send all results, not just linter errors
|
|
|
|
workerConfig.sendAll = true;
|
|
|
|
} else {
|
|
|
|
// Use default formatter
|
|
|
|
formatter = cli.getFormatter();
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:44:58 +02:00
|
|
|
// Check if outputting ESLint report to a file instead of stdout
|
2016-03-10 04:40:54 +01:00
|
|
|
i = process.argv.indexOf('-o');
|
|
|
|
if (i !== -1) {
|
|
|
|
if (!process.argv[i + 1])
|
|
|
|
throw new Error('Missing output filename');
|
2017-11-18 19:15:20 +01:00
|
|
|
const outPath = path.resolve(process.argv[i + 1]);
|
2016-03-10 04:40:54 +01:00
|
|
|
fd = fs.openSync(outPath, 'w');
|
|
|
|
outFn = function(str) {
|
|
|
|
fs.writeSync(fd, str, 'utf8');
|
|
|
|
};
|
2019-03-02 07:47:47 +01:00
|
|
|
process.on('exit', () => { fs.closeSync(fd); });
|
2016-03-10 04:40:54 +01:00
|
|
|
} else {
|
|
|
|
outFn = function(str) {
|
|
|
|
process.stdout.write(str);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the rest of the arguments as paths to lint, ignoring any unknown
|
|
|
|
// flags
|
|
|
|
for (i = 2; i < process.argv.length; ++i) {
|
|
|
|
if (process.argv[i][0] === '-') {
|
|
|
|
switch (process.argv[i]) {
|
|
|
|
case '-f': // Skip format name
|
|
|
|
case '-o': // Skip filename
|
|
|
|
case '-j': // Skip parallel job count number
|
|
|
|
++i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
paths.push(process.argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paths.length === 0)
|
|
|
|
return;
|
|
|
|
totalPaths = paths.length;
|
|
|
|
|
|
|
|
if (showProgress) {
|
|
|
|
// Start the progress display update timer when the first worker is ready
|
2019-03-02 07:47:47 +01:00
|
|
|
cluster.once('online', () => {
|
2016-03-10 04:40:54 +01:00
|
|
|
startTime = process.hrtime();
|
|
|
|
setInterval(printProgress, 1000).unref();
|
|
|
|
printProgress();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:47:47 +01:00
|
|
|
cluster.on('online', (worker) => {
|
2016-03-10 04:40:54 +01:00
|
|
|
// Configure worker and give it some initial work to do
|
|
|
|
worker.send(workerConfig);
|
|
|
|
sendWork(worker);
|
|
|
|
});
|
|
|
|
|
2019-03-02 07:47:47 +01:00
|
|
|
process.on('exit', (code) => {
|
2016-03-10 04:40:54 +01:00
|
|
|
if (showProgress) {
|
|
|
|
curPath = 'Done';
|
|
|
|
printProgress();
|
|
|
|
outFn('\r\n');
|
|
|
|
}
|
2016-04-27 07:12:56 +02:00
|
|
|
if (code === 0)
|
|
|
|
process.exit(failures ? 1 : 0);
|
2016-03-10 04:40:54 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
for (i = 0; i < numCPUs; ++i)
|
2016-04-27 07:12:56 +02:00
|
|
|
cluster.fork().on('message', onWorkerMessage).on('exit', onWorkerExit);
|
2016-04-15 07:25:21 +02:00
|
|
|
|
|
|
|
function onWorkerMessage(results) {
|
|
|
|
if (typeof results !== 'number') {
|
|
|
|
// The worker sent us results that are not all successes
|
2016-04-27 07:12:56 +02:00
|
|
|
if (workerConfig.sendAll) {
|
|
|
|
failures += results.errorCount;
|
|
|
|
results = results.results;
|
|
|
|
} else {
|
2016-04-15 07:25:21 +02:00
|
|
|
failures += results.length;
|
2016-04-27 07:12:56 +02:00
|
|
|
}
|
2017-10-06 18:59:28 +02:00
|
|
|
outFn(`${formatter(results)}\r\n`);
|
2016-04-15 07:25:21 +02:00
|
|
|
printProgress();
|
|
|
|
} else {
|
|
|
|
successes += results;
|
|
|
|
}
|
|
|
|
// Try to give the worker more work to do
|
|
|
|
sendWork(this);
|
|
|
|
}
|
2016-03-10 04:40:54 +01:00
|
|
|
|
2016-04-27 07:12:56 +02:00
|
|
|
function onWorkerExit(code, signal) {
|
|
|
|
if (code !== 0 || signal)
|
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
|
2016-03-10 04:40:54 +01:00
|
|
|
function sendWork(worker) {
|
|
|
|
if (!files || !files.length) {
|
|
|
|
// We either just started or we have no more files to lint for the current
|
|
|
|
// path. Find the next path that has some files to be linted.
|
|
|
|
while (paths.length) {
|
2019-03-02 15:07:03 +01:00
|
|
|
let dir = paths.shift();
|
2016-03-10 04:40:54 +01:00
|
|
|
curPath = dir;
|
|
|
|
const patterns = cli.resolveFileGlobPatterns([dir]);
|
|
|
|
dir = path.resolve(patterns[0]);
|
|
|
|
files = glob.sync(dir, globOptions);
|
|
|
|
if (files.length)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((!files || !files.length) && !paths.length) {
|
|
|
|
// We exhausted all input paths and thus have nothing left to do, so end
|
|
|
|
// the worker
|
|
|
|
return worker.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Give the worker an equal portion of the work left for the current path,
|
|
|
|
// but not exceeding a maximum file count in order to help keep *all*
|
|
|
|
// workers busy most of the time instead of only a minority doing most of
|
|
|
|
// the work.
|
|
|
|
const sliceLen = Math.min(maxWorkload, Math.ceil(files.length / numCPUs));
|
2019-03-02 15:07:03 +01:00
|
|
|
let slice;
|
2016-03-10 04:40:54 +01:00
|
|
|
if (sliceLen === files.length) {
|
2018-10-07 03:09:29 +02:00
|
|
|
// Micro-optimization to avoid splicing to an empty array
|
2016-03-10 04:40:54 +01:00
|
|
|
slice = files;
|
|
|
|
files = null;
|
|
|
|
} else {
|
|
|
|
slice = files.splice(0, sliceLen);
|
|
|
|
}
|
|
|
|
worker.send(slice);
|
|
|
|
}
|
|
|
|
|
|
|
|
function printProgress() {
|
|
|
|
if (!showProgress)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Clear line
|
2017-10-06 18:59:28 +02:00
|
|
|
outFn(`\r ${' '.repeat(lastLineLen)}\r`);
|
2016-03-10 04:40:54 +01:00
|
|
|
|
|
|
|
// Calculate and format the data for displaying
|
|
|
|
const elapsed = process.hrtime(startTime)[0];
|
2017-11-18 18:26:51 +01:00
|
|
|
const mins = `${Math.floor(elapsed / 60)}`.padStart(2, '0');
|
|
|
|
const secs = `${elapsed % 60}`.padStart(2, '0');
|
|
|
|
const passed = `${successes}`.padStart(6);
|
|
|
|
const failed = `${failures}`.padStart(6);
|
2019-03-02 15:07:03 +01:00
|
|
|
let pct = `${Math.ceil(((totalPaths - paths.length) / totalPaths) * 100)}`;
|
|
|
|
pct = pct.padStart(3);
|
2016-03-10 04:40:54 +01:00
|
|
|
|
2019-03-02 15:07:03 +01:00
|
|
|
let line = `[${mins}:${secs}|%${pct}|+${passed}|-${failed}]: ${curPath}`;
|
2016-03-10 04:40:54 +01:00
|
|
|
|
|
|
|
// Truncate line like cpplint does in case it gets too long
|
|
|
|
if (line.length > 75)
|
2017-10-06 18:59:28 +02:00
|
|
|
line = `${line.slice(0, 75)}...`;
|
2016-03-10 04:40:54 +01:00
|
|
|
|
|
|
|
// Store the line length so we know how much to erase the next time around
|
|
|
|
lastLineLen = line.length;
|
|
|
|
|
|
|
|
outFn(line);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Worker
|
|
|
|
|
2019-03-02 15:07:03 +01:00
|
|
|
let config = {};
|
2019-03-02 07:47:47 +01:00
|
|
|
process.on('message', (files) => {
|
2016-03-10 04:40:54 +01:00
|
|
|
if (files instanceof Array) {
|
|
|
|
// Lint some files
|
|
|
|
const report = cli.executeOnFiles(files);
|
2016-04-30 01:47:18 +02:00
|
|
|
|
|
|
|
// If we were asked to fix the fixable issues, do so.
|
|
|
|
if (cliOptions.fix)
|
|
|
|
CLIEngine.outputFixes(report);
|
|
|
|
|
2016-03-10 04:40:54 +01:00
|
|
|
if (config.sendAll) {
|
|
|
|
// Return both success and error results
|
|
|
|
|
|
|
|
const results = report.results;
|
|
|
|
// Silence warnings for files with no errors while keeping the "ok"
|
|
|
|
// status
|
|
|
|
if (report.warningCount > 0) {
|
2019-03-02 15:07:03 +01:00
|
|
|
for (let i = 0; i < results.length; ++i) {
|
2016-03-10 04:40:54 +01:00
|
|
|
const result = results[i];
|
|
|
|
if (result.errorCount === 0 && result.warningCount > 0) {
|
|
|
|
result.warningCount = 0;
|
|
|
|
result.messages = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 07:12:56 +02:00
|
|
|
process.send({ results: results, errorCount: report.errorCount });
|
2016-03-10 04:40:54 +01:00
|
|
|
} else if (report.errorCount === 0) {
|
|
|
|
// No errors, return number of successful lint operations
|
|
|
|
process.send(files.length);
|
|
|
|
} else {
|
|
|
|
// One or more errors, return the error results only
|
|
|
|
process.send(CLIEngine.getErrorResults(report.results));
|
|
|
|
}
|
|
|
|
} else if (typeof files === 'object') {
|
|
|
|
// The master process is actually sending us our configuration and not a
|
|
|
|
// list of files to lint
|
|
|
|
config = files;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|