forked from AtomLinter/linter-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
139 lines (126 loc) · 4.11 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
// Note: 'use babel' doesn't work in forked processes
process.title = 'linter-eslint helper'
const CP = require('childprocess-promise')
const ChildProcess = require('child_process')
const Path = require('path')
const FS = require('fs')
const resolveEnv = require('resolve-env')
const Communication = new CP()
let eslintPath = null
const eslintPathLocal = Path.join(FS.realpathSync(Path.join(__dirname, '..')), 'node_modules', 'eslint')
let eslint = null
let prefixPath = null
function find(startDir, names) {
let localNames;
if (typeof names === 'string') {
localNames = [names]
} else {
localNames = names
}
const chunks = startDir.split(Path.sep)
while (chunks.length) {
const currentDirectory = Path.join(chunks.join(Path.sep))
for (let index = 0; index < localNames.length; index++) {
const filePath = Path.join(currentDirectory, localNames[index])
try {
FS.accessSync(filePath, FS.R_OK)
return filePath
} catch (_) { }
}
chunks.pop()
}
return null
}
Communication.on('JOB', function(job) {
const params = job.Message
let configFile = null
let configInPackage = false
global.__LINTER_RESPONSE = []
configFile = find(params.fileDir, ['.eslintrc.js', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', '.eslintrc'])
if (!configFile) {
const packagePath = find(params.fileDir, 'package.json')
if (packagePath) {
configInPackage = Boolean(require(packagePath).eslintConfig)
}
}
if (params.canDisable && !configFile && !configInPackage) {
job.Response = []
return
} else if (params.configFile) {
configFile = params.configFile
}
const modulesPath = find(params.fileDir, 'node_modules')
const eslintignoreDir = Path.dirname(find(params.fileDir, '.eslintignore'))
let eslintNewPath = null
if (modulesPath) {
process.env.NODE_PATH = modulesPath
} else process.env.NODE_PATH = ''
require('module').Module._initPaths()
if (params.global) {
if (params.nodePath === '' && prefixPath === null) {
const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'
try {
prefixPath = ChildProcess.spawnSync(npmCommand, ['get', 'prefix']).output[1].toString().trim()
} catch (e) {
throw new Error('Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly')
}
}
if (process.platform === 'win32') {
eslintNewPath = Path.join(params.nodePath || prefixPath, 'node_modules', 'eslint')
} else {
eslintNewPath = Path.join(params.nodePath || prefixPath, 'lib', 'node_modules', 'eslint')
}
} else {
try {
FS.accessSync(eslintNewPath = Path.join(modulesPath, 'eslint'), FS.R_OK)
} catch (_) {
eslintNewPath = eslintPathLocal
}
}
if (eslintNewPath !== eslintPath) {
try {
eslint = require(Path.join(eslintNewPath, 'lib', 'cli.js'))
eslintPath = eslintNewPath
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new Error('ESLint not found, Please install or make sure Atom is getting $PATH correctly')
} else throw e
}
}
job.Response = new Promise(function(resolve) {
let filePath
if (eslintignoreDir) {
filePath = Path.relative(eslintignoreDir, params.filePath)
process.chdir(eslintignoreDir)
} else {
filePath = Path.basename(params.filePath)
process.chdir(params.fileDir)
}
const argv = [
process.execPath,
eslintPath,
'--stdin',
'--format',
Path.join(__dirname, 'reporter.js')
]
if (params.rulesDir) {
let rulesDir = resolveEnv(params.rulesDir)
if (!Path.isAbsolute(rulesDir)) {
rulesDir = find(params.fileDir, rulesDir)
}
argv.push('--rulesdir', rulesDir)
}
if (configFile !== null) {
argv.push('--config', resolveEnv(configFile))
}
if (params.disableIgnores) {
argv.push('--no-ignore')
}
argv.push('--stdin-filename', filePath)
process.argv = argv
eslint.execute(process.argv, params.contents)
resolve(global.__LINTER_RESPONSE)
})
})
process.exit = function() { /* Stop eslint from closing the daemon */ }