|
3 | 3 | process.title = 'linter-eslint helper'
|
4 | 4 |
|
5 | 5 | const CP = require('childprocess-promise')
|
6 |
| -const ChildProcess = require('child_process') |
| 6 | +const execFileSync = require('child_process').execFileSync |
7 | 7 | const Path = require('path')
|
8 |
| -const FS = require('fs') |
| 8 | + |
9 | 9 | const resolveEnv = require('resolve-env')
|
| 10 | +const Helpers = require('./es5-helpers') |
10 | 11 |
|
| 12 | +const findEslintDir = Helpers.findEslintDir |
| 13 | +const find = Helpers.find |
| 14 | +const determineConfigFile = Helpers.determineConfigFile |
| 15 | +const getEslintCli = Helpers.getEslintCli |
11 | 16 | const Communication = new CP()
|
12 | 17 |
|
| 18 | +// closed-over module-scope variables |
13 | 19 | let eslintPath = null
|
14 |
| -const eslintPathLocal = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint') |
15 | 20 | let eslint = null
|
16 |
| -let prefixPath = null |
17 |
| - |
18 |
| -function find(startDir, names) { |
19 |
| - let localNames; |
20 |
| - if (typeof names === 'string') { |
21 |
| - localNames = [names] |
22 |
| - } else { |
23 |
| - localNames = names |
24 |
| - } |
25 |
| - const chunks = startDir.split(Path.sep) |
26 |
| - while (chunks.length) { |
27 |
| - const currentDirectory = Path.join(chunks.join(Path.sep)) |
28 |
| - for (let index = 0; index < localNames.length; index++) { |
29 |
| - const filePath = Path.join(currentDirectory, localNames[index]) |
30 |
| - try { |
31 |
| - FS.accessSync(filePath, FS.R_OK) |
32 |
| - return filePath |
33 |
| - } catch (_) { } |
34 |
| - } |
35 |
| - |
36 |
| - chunks.pop() |
37 |
| - } |
38 |
| - return null |
39 |
| -} |
40 | 21 |
|
41 | 22 | Communication.on('JOB', function(job) {
|
42 | 23 | const params = job.Message
|
| 24 | + const modulesPath = find(params.fileDir, 'node_modules') |
| 25 | + const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore')) |
43 | 26 | let configFile = null
|
44 |
| - let configInPackage = false |
45 | 27 | global.__LINTER_RESPONSE = []
|
46 | 28 |
|
47 |
| - configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc']) |
48 |
| - if (!configFile) { |
49 |
| - const packagePath = find(params.fileDir, 'package.json') |
50 |
| - if (packagePath) { |
51 |
| - configInPackage = Boolean(require(packagePath).eslintConfig) |
52 |
| - } |
53 |
| - } |
54 |
| - if (params.canDisable && !configFile && !configInPackage) { |
| 29 | + // Check for config file and determine whether to bail out |
| 30 | + configFile = determineConfigFile(params) |
| 31 | + |
| 32 | + if (params.canDisable && configFile === null) { |
55 | 33 | job.Response = []
|
56 | 34 | return
|
57 |
| - } else if (params.configFile) { |
58 |
| - configFile = params.configFile |
59 | 35 | }
|
60 | 36 |
|
61 |
| - const modulesPath = find(params.fileDir, 'node_modules') |
62 |
| - const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore')) |
63 |
| - let eslintNewPath = null |
64 | 37 | if (modulesPath) {
|
65 | 38 | process.env.NODE_PATH = modulesPath
|
66 | 39 | } else process.env.NODE_PATH = ''
|
67 | 40 | require('module').Module._initPaths()
|
68 | 41 |
|
69 |
| - |
70 |
| - if (params.global) { |
71 |
| - if (params.nodePath === '' && prefixPath === null) { |
72 |
| - const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm' |
73 |
| - try { |
74 |
| - prefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim() |
75 |
| - } catch (e) { |
76 |
| - throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly') |
77 |
| - } |
78 |
| - } |
79 |
| - if (process.platform === 'win32') { |
80 |
| - eslintNewPath = Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint') |
81 |
| - } else { |
82 |
| - eslintNewPath = Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint') |
83 |
| - } |
84 |
| - } else { |
85 |
| - try { |
86 |
| - FS.accessSync(eslintNewPath = Path.join(modulesPath, 'eslint'), FS.R_OK) |
87 |
| - } catch (_) { |
88 |
| - eslintNewPath = eslintPathLocal |
89 |
| - } |
90 |
| - } |
91 |
| - |
| 42 | + // Determine which eslint instance to use |
| 43 | + const eslintNewPath = findEslintDir(params) |
92 | 44 | if (eslintNewPath !== eslintPath) {
|
93 |
| - try { |
94 |
| - eslint = require(Path.join(eslintNewPath, 'lib', 'cli.js')) |
95 |
| - eslintPath = eslintNewPath |
96 |
| - } catch (e) { |
97 |
| - if (e.code === 'MODULE_NOT_FOUND') { |
98 |
| - throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly') |
99 |
| - } else throw e |
100 |
| - } |
| 45 | + eslint = getEslintCli(eslintNewPath) |
| 46 | + eslintPath = eslintNewPath |
101 | 47 | }
|
102 | 48 |
|
103 | 49 | job.Response = new Promise(function(resolve) {
|
@@ -136,4 +82,28 @@ Communication.on('JOB', function(job) {
|
136 | 82 | })
|
137 | 83 | })
|
138 | 84 |
|
| 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')) |
| 90 | + |
| 91 | + const argv = [ |
| 92 | + params.filePath, |
| 93 | + '--fix' |
| 94 | + ] |
| 95 | + if (configFile !== null) { |
| 96 | + argv.push('--config', resolveEnv(configFile)) |
| 97 | + } |
| 98 | + |
| 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 | + }) |
| 107 | +}) |
| 108 | + |
139 | 109 | process.exit = function() { /* Stop eslint from closing the daemon */ }
|
0 commit comments