@@ -5,6 +5,22 @@ Object.defineProperty(exports, "__esModule", {
5
5
value : true
6
6
} ) ;
7
7
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 ) ;
8
24
9
25
var _child_process = require ( 'child_process' ) ;
10
26
@@ -14,6 +30,8 @@ var _childprocessPromise = require('childprocess-promise');
14
30
15
31
var _childprocessPromise2 = _interopRequireDefault ( _childprocessPromise ) ;
16
32
33
+ var _atomLinter = require ( 'atom-linter' ) ;
34
+
17
35
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
18
36
19
37
function spawnWorker ( ) {
@@ -46,4 +64,107 @@ function spawnWorker() {
46
64
killer ( ) ;
47
65
process . removeListener ( 'exit' , killer ) ;
48
66
} } } ;
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 } ;
49
170
}
0 commit comments