| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | const {
|
|---|
| 4 | cpus
|
|---|
| 5 | } = require('os');
|
|---|
| 6 |
|
|---|
| 7 | const {
|
|---|
| 8 | Worker: JestWorker
|
|---|
| 9 | } = require('jest-worker');
|
|---|
| 10 |
|
|---|
| 11 | const {
|
|---|
| 12 | getESLintOptions
|
|---|
| 13 | } = require('./options');
|
|---|
| 14 |
|
|---|
| 15 | const {
|
|---|
| 16 | jsonStringifyReplacerSortKeys
|
|---|
| 17 | } = require('./utils');
|
|---|
| 18 | /** @type {{[key: string]: any}} */
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | const cache = {};
|
|---|
| 22 | /** @typedef {import('eslint').ESLint} ESLint */
|
|---|
| 23 |
|
|---|
| 24 | /** @typedef {import('eslint').ESLint.LintResult} LintResult */
|
|---|
| 25 |
|
|---|
| 26 | /** @typedef {import('./options').Options} Options */
|
|---|
| 27 |
|
|---|
| 28 | /** @typedef {() => Promise<void>} AsyncTask */
|
|---|
| 29 |
|
|---|
| 30 | /** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
|
|---|
| 31 |
|
|---|
| 32 | /** @typedef {{threads: number, ESLint: ESLint, eslint: ESLint, lintFiles: LintTask, cleanup: AsyncTask}} Linter */
|
|---|
| 33 |
|
|---|
| 34 | /** @typedef {JestWorker & {lintFiles: LintTask}} Worker */
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * @param {Options} options
|
|---|
| 38 | * @returns {Linter}
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | function loadESLint(options) {
|
|---|
| 42 | const {
|
|---|
| 43 | eslintPath
|
|---|
| 44 | } = options;
|
|---|
| 45 |
|
|---|
| 46 | const {
|
|---|
| 47 | ESLint
|
|---|
| 48 | } = require(eslintPath || 'eslint'); // Filter out loader options before passing the options to ESLint.
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | const eslint = new ESLint(getESLintOptions(options));
|
|---|
| 52 | return {
|
|---|
| 53 | threads: 1,
|
|---|
| 54 | ESLint,
|
|---|
| 55 | eslint,
|
|---|
| 56 | lintFiles: async files => {
|
|---|
| 57 | const results = await eslint.lintFiles(files); // istanbul ignore else
|
|---|
| 58 |
|
|---|
| 59 | if (options.fix) {
|
|---|
| 60 | await ESLint.outputFixes(results);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | return results;
|
|---|
| 64 | },
|
|---|
| 65 | // no-op for non-threaded
|
|---|
| 66 | cleanup: async () => {}
|
|---|
| 67 | };
|
|---|
| 68 | }
|
|---|
| 69 | /**
|
|---|
| 70 | * @param {string|undefined} key
|
|---|
| 71 | * @param {number} poolSize
|
|---|
| 72 | * @param {Options} options
|
|---|
| 73 | * @returns {Linter}
|
|---|
| 74 | */
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | function loadESLintThreaded(key, poolSize, options) {
|
|---|
| 78 | const cacheKey = getCacheKey(key, options);
|
|---|
| 79 | const {
|
|---|
| 80 | eslintPath = 'eslint'
|
|---|
| 81 | } = options;
|
|---|
| 82 |
|
|---|
| 83 | const source = require.resolve('./worker');
|
|---|
| 84 |
|
|---|
| 85 | const workerOptions = {
|
|---|
| 86 | enableWorkerThreads: true,
|
|---|
| 87 | numWorkers: poolSize,
|
|---|
| 88 | setupArgs: [{
|
|---|
| 89 | eslintPath,
|
|---|
| 90 | eslintOptions: getESLintOptions(options)
|
|---|
| 91 | }]
|
|---|
| 92 | };
|
|---|
| 93 | const local = loadESLint(options);
|
|---|
| 94 | let worker =
|
|---|
| 95 | /** @type {Worker?} */
|
|---|
| 96 | new JestWorker(source, workerOptions);
|
|---|
| 97 | /** @type {Linter} */
|
|---|
| 98 |
|
|---|
| 99 | const context = { ...local,
|
|---|
| 100 | threads: poolSize,
|
|---|
| 101 | lintFiles: async files => worker && (await worker.lintFiles(files)) ||
|
|---|
| 102 | /* istanbul ignore next */
|
|---|
| 103 | [],
|
|---|
| 104 | cleanup: async () => {
|
|---|
| 105 | cache[cacheKey] = local;
|
|---|
| 106 |
|
|---|
| 107 | context.lintFiles = files => local.lintFiles(files);
|
|---|
| 108 |
|
|---|
| 109 | if (worker) {
|
|---|
| 110 | worker.end();
|
|---|
| 111 | worker = null;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | };
|
|---|
| 115 | return context;
|
|---|
| 116 | }
|
|---|
| 117 | /**
|
|---|
| 118 | * @param {string|undefined} key
|
|---|
| 119 | * @param {Options} options
|
|---|
| 120 | * @returns {Linter}
|
|---|
| 121 | */
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | function getESLint(key, {
|
|---|
| 125 | threads,
|
|---|
| 126 | ...options
|
|---|
| 127 | }) {
|
|---|
| 128 | const max = typeof threads !== 'number' ? threads ? cpus().length - 1 : 1 :
|
|---|
| 129 | /* istanbul ignore next */
|
|---|
| 130 | threads;
|
|---|
| 131 | const cacheKey = getCacheKey(key, {
|
|---|
| 132 | threads,
|
|---|
| 133 | ...options
|
|---|
| 134 | });
|
|---|
| 135 |
|
|---|
| 136 | if (!cache[cacheKey]) {
|
|---|
| 137 | cache[cacheKey] = max > 1 ? loadESLintThreaded(key, max, options) : loadESLint(options);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | return cache[cacheKey];
|
|---|
| 141 | }
|
|---|
| 142 | /**
|
|---|
| 143 | * @param {string|undefined} key
|
|---|
| 144 | * @param {Options} options
|
|---|
| 145 | * @returns {string}
|
|---|
| 146 | */
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | function getCacheKey(key, options) {
|
|---|
| 150 | return JSON.stringify({
|
|---|
| 151 | key,
|
|---|
| 152 | options
|
|---|
| 153 | }, jsonStringifyReplacerSortKeys);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | module.exports = {
|
|---|
| 157 | loadESLint,
|
|---|
| 158 | loadESLintThreaded,
|
|---|
| 159 | getESLint
|
|---|
| 160 | }; |
|---|