Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit d34746f

Browse files
author
steelbrain
committed
🎨 Further simplify worker
Move more logic to helpers
1 parent 0e3cf19 commit d34746f

File tree

5 files changed

+191
-161
lines changed

5 files changed

+191
-161
lines changed

lib/es5-helpers.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

lib/helpers.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ Object.defineProperty(exports, "__esModule", {
55
value: true
66
});
77
exports.spawnWorker = spawnWorker;
8+
exports.getModulesDirectory = getModulesDirectory;
9+
exports.getIgnoresFile = getIgnoresFile;
10+
exports.getEslintFromDirectory = getEslintFromDirectory;
11+
exports.getNodePrefixPath = getNodePrefixPath;
12+
exports.getBundledEslintDirectory = getBundledEslintDirectory;
13+
exports.getEslintDirectory = getEslintDirectory;
14+
exports.getEslintConfig = getEslintConfig;
15+
exports.getEslint = getEslint;
16+
17+
var _path = require('path');
18+
19+
var _path2 = _interopRequireDefault(_path);
20+
21+
var _fs = require('fs');
22+
23+
var _fs2 = _interopRequireDefault(_fs);
824

925
var _child_process = require('child_process');
1026

@@ -14,6 +30,8 @@ var _childprocessPromise = require('childprocess-promise');
1430

1531
var _childprocessPromise2 = _interopRequireDefault(_childprocessPromise);
1632

33+
var _atomLinter = require('atom-linter');
34+
1735
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1836

1937
function spawnWorker() {
@@ -46,4 +64,107 @@ function spawnWorker() {
4664
killer();
4765
process.removeListener('exit', killer);
4866
} } };
67+
}
68+
69+
function getModulesDirectory(fileDir) {
70+
return (0, _atomLinter.findFile)(fileDir, 'node_modules');
71+
}
72+
73+
function getIgnoresFile(fileDir) {
74+
return _path2.default.dirname((0, _atomLinter.findFile)(fileDir, '.eslintignore'));
75+
}
76+
77+
function getEslintFromDirectory(path) {
78+
try {
79+
return require(_path2.default.join(path, 'lib', 'cli.js'));
80+
} catch (e) {
81+
if (e.code === 'MODULE_NOT_FOUND') {
82+
throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly');
83+
} else throw e;
84+
}
85+
}
86+
87+
let nodePrefixPath = null;
88+
89+
function getNodePrefixPath() {
90+
if (nodePrefixPath === null) {
91+
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm';
92+
try {
93+
nodePrefixPath = _child_process2.default.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim();
94+
} catch (e) {
95+
throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly');
96+
}
97+
}
98+
return nodePrefixPath;
99+
}
100+
101+
let bundledEslintDirectory = null;
102+
103+
function getBundledEslintDirectory() {
104+
if (bundledEslintDirectory === null) {
105+
bundledEslintDirectory = _path2.default.normalize(_path2.default.join(__dirname, '..', 'node_modules', 'eslint'));
106+
}
107+
return bundledEslintDirectory;
108+
}
109+
110+
function getEslintDirectory(params) {
111+
let modulesPath = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
112+
113+
if (params.global) {
114+
const prefixPath = getNodePrefixPath();
115+
if (process.platform === 'win32') {
116+
return _path2.default.join(params.nodePath || prefixPath, 'node_modules', 'eslint');
117+
} else {
118+
return _path2.default.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint');
119+
}
120+
} else {
121+
const eslintPath = _path2.default.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint');
122+
try {
123+
_fs2.default.accessSync(eslintPath, _fs2.default.R_OK);
124+
return eslintPath;
125+
} catch (_) {
126+
return getBundledEslintDirectory();
127+
}
128+
}
129+
}
130+
131+
function getEslintConfig(params) {
132+
const configFile = (0, _atomLinter.findFile)(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) || null;
133+
if (configFile) {
134+
return configFile;
135+
}
136+
137+
const packagePath = (0, _atomLinter.findFile)(params.fileDir, 'package.json');
138+
if (packagePath && Boolean(require(packagePath).eslintConfig)) {
139+
return packagePath;
140+
}
141+
142+
if (params.canDisable) {
143+
return null;
144+
}
145+
146+
if (params.configFile) {
147+
return params.configFile;
148+
}
149+
}
150+
151+
let eslint;
152+
let lastEslintDirectory;
153+
let lastModulesPath;
154+
155+
function getEslint(params) {
156+
const modulesPath = getModulesDirectory(params.fileDir);
157+
const eslintDirectory = getEslintDirectory(params, modulesPath);
158+
if (eslintDirectory !== lastEslintDirectory) {
159+
lastEslintDirectory = eslintDirectory;
160+
eslint = getEslintFromDirectory(eslintDirectory);
161+
}
162+
if (lastModulesPath !== modulesPath) {
163+
lastModulesPath = modulesPath;
164+
if (modulesPath) {
165+
process.env.NODE_PATH = modulesPath;
166+
} else process.env.NODE_PATH = '';
167+
require('module').Module._initPaths();
168+
}
169+
return { eslint: eslint, eslintDirectory: eslintDirectory };
49170
}

lib/worker.js

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,40 @@
22
// Note: 'use babel' doesn't work in forked processes
33
process.title = 'linter-eslint helper'
44

5-
const CP = require('childprocess-promise')
6-
const execFileSync = require('child_process').execFileSync
7-
const Path = require('path')
5+
import Path from 'path'
6+
import {execFileSync} from 'child_process'
7+
import CP from 'childprocess-promise'
8+
import resolveEnv from 'resolve-end'
9+
import * as Helpers from './helpers'
810

9-
const resolveEnv = require('resolve-env')
10-
const Helpers = require('./es5-helpers')
11-
12-
const findEslintDir = Helpers.findEslintDir
13-
const find = Helpers.find
14-
const determineConfigFile = Helpers.determineConfigFile
15-
const getEslintCli = Helpers.getEslintCli
1611
const Communication = new CP()
1712

18-
// closed-over module-scope variables
19-
let eslintPath = null
20-
let eslint = null
21-
22-
Communication.on('JOB', function(job) {
23-
const params = job.Message
24-
const modulesPath = find(params.fileDir, 'node_modules')
25-
const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore'))
26-
let configFile = null
13+
Communication.on('JOB', function(Job) {
2714
global.__LINTER_RESPONSE = []
2815

29-
// Check for config file and determine whether to bail out
30-
configFile = determineConfigFile(params)
16+
const params = Job.Message
17+
const ignoreFile = Helpers.getIgnoresFile(params.fileDir)
18+
const configFile = Helpers.getEslintConfig(params.fileDir)
19+
const {eslint, eslintDirectory} = Helpers.getEslint(params)
3120

3221
if (params.canDisable && configFile === null) {
33-
job.Response = []
34-
return
22+
return Job.Response = []
3523
}
3624

37-
if (modulesPath) {
38-
process.env.NODE_PATH = modulesPath
39-
} else process.env.NODE_PATH = ''
40-
require('module').Module._initPaths()
4125

42-
// Determine which eslint instance to use
43-
const eslintNewPath = findEslintDir(params)
44-
if (eslintNewPath !== eslintPath) {
45-
eslint = getEslintCli(eslintNewPath)
46-
eslintPath = eslintNewPath
47-
}
48-
49-
job.Response = new Promise(function(resolve) {
26+
Job.Response = new Promise(function(resolve) {
5027
let filePath
51-
if (eslintignoreDir) {
52-
filePath = Path.relative(eslintignoreDir, params.filePath)
53-
process.chdir(eslintignoreDir)
28+
if (ignoreFile) {
29+
filePath = Path.relative(ignoreFile, params.filePath)
30+
process.chdir(ignoreFile)
5431
} else {
5532
filePath = Path.basename(params.filePath)
5633
process.chdir(params.fileDir)
5734
}
35+
5836
const argv = [
5937
process.execPath,
60-
eslintPath,
38+
eslintDirectory,
6139
'--stdin',
6240
'--format',
6341
Path.join(__dirname, 'reporter.js')
@@ -77,33 +55,35 @@ Communication.on('JOB', function(job) {
7755
}
7856
argv.push('--stdin-filename', filePath)
7957
process.argv = argv
58+
8059
eslint.execute(process.argv, params.contents)
8160
resolve(global.__LINTER_RESPONSE)
8261
})
8362
})
8463

85-
Communication.on('FIX', function(fixJob) {
86-
const params = fixJob.Message
87-
const eslintDir = findEslintDir(params)
88-
const configFile = determineConfigFile(params)
89-
const eslintBinPath = Path.normalize(Path.join(eslintDir, 'bin', 'eslint.js'))
64+
Communication.on('FIX', function(Job) {
65+
const params = Job.Message
66+
const {eslint, eslintDirectory} = Helpers.getEslint(params)
67+
const configFile = Helpers.getEslintConfig(params)
9068

9169
const argv = [
70+
process.execPath,
71+
eslintDirectory,
9272
params.filePath,
9373
'--fix'
9474
]
9575
if (configFile !== null) {
9676
argv.push('--config', resolveEnv(configFile))
9777
}
78+
process.argv = argv
79+
process.chdir(params.fileDir)
9880

99-
fixJob.Response = new Promise(function(resolve, reject) {
100-
try {
101-
execFileSync(eslintBinPath, argv, {cwd: params.fileDir})
102-
} catch (err) {
103-
reject('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain')
104-
}
105-
resolve('Linter-ESLint: Fix Complete')
106-
})
81+
try {
82+
eslint.execute(process.argv)
83+
} catch (_) {
84+
throw new Error('Linter-ESLint: Fix Attempt Completed, Linting Errors Remain')
85+
}
86+
return 'Linter-ESLint: Fix Complete'
10787
})
10888

10989
process.exit = function() { /* Stop eslint from closing the daemon */ }

src/helpers.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function getBundledEslintDirectory() {
7979
return bundledEslintDirectory
8080
}
8181

82-
export function getEslintDirectory(params, modulesPath) {
82+
export function getEslintDirectory(params, modulesPath = null) {
8383
if (params.global) {
8484
const prefixPath = getNodePrefixPath()
8585
if (process.platform === 'win32') {
@@ -88,7 +88,7 @@ export function getEslintDirectory(params, modulesPath) {
8888
return Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint')
8989
}
9090
} else {
91-
const eslintPath = Path.join(modulesPath, 'eslint')
91+
const eslintPath = Path.join(modulesPath || getModulesDirectory(params.fileDir), 'eslint')
9292
try {
9393
FS.accessSync(eslintPath, FS.R_OK)
9494
return eslintPath
@@ -117,3 +117,24 @@ export function getEslintConfig(params) {
117117
return params.configFile
118118
}
119119
}
120+
121+
let eslint
122+
let lastEslintDirectory
123+
let lastModulesPath
124+
125+
export function getEslint(params) {
126+
const modulesPath = getModulesDirectory(params.fileDir)
127+
const eslintDirectory = getEslintDirectory(params, modulesPath)
128+
if (eslintDirectory !== lastEslintDirectory) {
129+
lastEslintDirectory = eslintDirectory
130+
eslint = getEslintFromDirectory(eslintDirectory)
131+
}
132+
if (lastModulesPath !== modulesPath) {
133+
lastModulesPath = modulesPath
134+
if (modulesPath) {
135+
process.env.NODE_PATH = modulesPath
136+
} else process.env.NODE_PATH = ''
137+
require('module').Module._initPaths()
138+
}
139+
return {eslint, eslintDirectory}
140+
}

0 commit comments

Comments
 (0)