Skip to content

improvement: upgrade to eslint 7 and limit CPU count #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

9 changes: 9 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
["@babel/preset-env", {
"targets": {
"node": true
},
"loose": true
}]]
}
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
"eslint-parallel": "./lib/cli.js"
},
"dependencies": {
"babel-core": "^6.20.0",
"chalk": "^1.1.3",
"eslint": "^5.0.0",
"@babel/register": "^7.10.5",
"chalk": "^4.1.0",
"eslint": "7.5.0",
"strip-ansi": "^6.0.0",
"text-table": "^0.2.0"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-eslint": "^6.1.2",
"babel-preset-es2015": "^6.9.0"
"@babel/cli": "7.10.5",
"@babel/core": "7.10.5",
"@babel/preset-env": "7.10.4"
},
"peerDependencies": {
"eslint": ">=5.0.0"
"eslint": ">=7.0.0"
},
"scripts": {
"build": "node_modules/.bin/babel src --out-dir lib --copy-files --loose-mode",
"build": "node_modules/.bin/babel src --out-dir lib --copy-files",
"prepublishOnly": "npm run build"
}
}
9 changes: 5 additions & 4 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node
require('babel-register');
require('@babel/register');

/**
* NPM dependencies
Expand Down Expand Up @@ -31,7 +31,8 @@ function translateOptions(cliOptions) {
cacheFile: cliOptions.cacheFile,
cacheLocation: cliOptions.cacheLocation,
fix: cliOptions.fix,
allowInlineConfig: cliOptions.inlineConfig
allowInlineConfig: cliOptions.inlineConfig,
reportUnusedDisableDirectives: cliOptions.reportUnusedDisableDirectives
};
}

Expand All @@ -44,10 +45,10 @@ if (cliOptions.version) {
} else {
new Linter(translateOptions(cliOptions)).execute(cliOptions._).then(
(result) => {
const failed = result.errorCount || result.warningCount;
const failed = result.errorCount || (!cliOptions.quiet && result.warningCount);
console.log(formatTotal(result));

if (failed) {
console.log(formatTotal(result));
process.exit(1);
} else {
process.exit(0);
Expand Down
10 changes: 5 additions & 5 deletions src/formatter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import chalk from 'chalk';
import stripAnsi from 'strip-ansi';
import table from 'text-table';

export function formatTotal(results) {
const total = results.errorCount + results.warningCount;
const problemLabel = total && total === 1 ? 'problem' : 'problems';
const errorLabel = total && total === 1 ? 'error' : 'errors';
const warningLabel = total && total === 1 ? 'warning' : 'warnings';
return chalk.red.bold(
`\u2716 ${total} ${problemLabel} (${results.errorCount} ${errorLabel}, ${results.warningCount} ${warningLabel})\n`
);
const text = `${total} ${problemLabel} (${results.errorCount} ${errorLabel}, ${results.warningCount} ${warningLabel})\n`;
return results.errorCount > 0 ? chalk.red.bold(`\u2716 ${text}`) : results.warningCount > 0 ? chalk.yellow.bold(`\u2716 ${text}`) : ` ${text}`;
}

export function formatResults(results) {
Expand Down Expand Up @@ -46,13 +46,13 @@ export function formatResults(results) {
{
align: ['', 'r', 'l'],
stringLength(str) {
return chalk.stripColor(str).length;
return stripAnsi(str).length;
}
}
).split('\n').map(el => el.replace(/(\d+)\s+(\d+)/, (m, p1, p2) => chalk.dim(`${p1}:${p2}`))).join('\n')}\n\n`;
});

return output;
};
}

export default { formatResults, formatTotal };
28 changes: 21 additions & 7 deletions src/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@
**/
import fs from 'fs';
import path from 'path';
import os from 'os';
import { fork } from 'child_process';

/**
* NPM dependencies
**/
import { CLIEngine } from 'eslint';
import { listFilesToProcess } from 'eslint/lib/util/glob-utils.js';
import glob from 'glob';
import { FileEnumerator } from 'eslint/lib/cli-engine/file-enumerator';
import { CascadingConfigArrayFactory } from 'eslint/lib/cli-engine/cascading-config-array-factory';

/**
* Local dependencies
**/
import { formatResults } from './formatter';

const cpuCount = os.cpus().length;
const cpuCount = Number(process.env.ESLINT_CPU_COUNT) ?? os.cpus().length;

function listFilesToProcess(patterns, options) {
return Array.from(
new FileEnumerator({
...options,
configArrayFactory: new CascadingConfigArrayFactory({
...options,

// Disable "No Configuration Found" error.
useEslintrc: false
})
}).iterateFiles(patterns),
({ filePath, ignored }) => ({ filename: filePath, ignored })
);
}

function hasEslintCache(options) {
const cacheLocation = (
Expand Down Expand Up @@ -86,7 +100,7 @@ export default class Linter {
errorCount: 0,
warningCount: 0
};
const chunckedPromises = [];
const chunkedPromises = [];
const chunkSize = Math.ceil(files.length / cpuCount);
for (let i = 0; i < files.length; i += chunkSize) {
const chunkedPaths = files.slice(i, i + chunkSize);
Expand All @@ -97,9 +111,9 @@ export default class Linter {
totalCount.errorCount += report.errorCount;
totalCount.warningCount += report.warningCount;
});
chunckedPromises.push(chunckedPromise);
chunkedPromises.push(chunckedPromise);
}
Promise.all(chunckedPromises).then(() => {
Promise.all(chunkedPromises).then(() => {
resolve(totalCount);
});
} else {
Expand Down
Loading