123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- "use strict";
- const {
- cpus
- } = require('os');
- const {
- Worker: JestWorker
- } = require('jest-worker');
- const {
- getESLintOptions
- } = require('./options');
- const {
- jsonStringifyReplacerSortKeys
- } = require('./utils');
- const cache = {};
- function loadESLint(options) {
- const {
- eslintPath
- } = options;
- const {
- ESLint
- } = require(eslintPath || 'eslint');
- const eslint = new ESLint(getESLintOptions(options));
- return {
- threads: 1,
- ESLint,
- eslint,
- lintFiles: async files => {
- const results = await eslint.lintFiles(files);
- if (options.fix) {
- await ESLint.outputFixes(results);
- }
- return results;
- },
-
- cleanup: async () => {}
- };
- }
- function loadESLintThreaded(key, poolSize, options) {
- const cacheKey = getCacheKey(key, options);
- const {
- eslintPath = 'eslint'
- } = options;
- const source = require.resolve('./worker');
- const workerOptions = {
- enableWorkerThreads: true,
- numWorkers: poolSize,
- setupArgs: [{
- eslintPath,
- eslintOptions: getESLintOptions(options)
- }]
- };
- const local = loadESLint(options);
- let worker =
-
- new JestWorker(source, workerOptions);
-
- const context = { ...local,
- threads: poolSize,
- lintFiles: async files => worker && (await worker.lintFiles(files)) ||
-
- [],
- cleanup: async () => {
- cache[cacheKey] = local;
- context.lintFiles = files => local.lintFiles(files);
- if (worker) {
- worker.end();
- worker = null;
- }
- }
- };
- return context;
- }
- function getESLint(key, {
- threads,
- ...options
- }) {
- const max = typeof threads !== 'number' ? threads ? cpus().length - 1 : 1 :
-
- threads;
- const cacheKey = getCacheKey(key, {
- threads,
- ...options
- });
- if (!cache[cacheKey]) {
- cache[cacheKey] = max > 1 ? loadESLintThreaded(key, max, options) : loadESLint(options);
- }
- return cache[cacheKey];
- }
- function getCacheKey(key, options) {
- return JSON.stringify({
- key,
- options
- }, jsonStringifyReplacerSortKeys);
- }
- module.exports = {
- loadESLint,
- loadESLintThreaded,
- getESLint
- };
|