@@ -4,8 +4,7 @@ import Path from 'path'
4
4
import FS from 'fs'
5
5
import ChildProcess from 'child_process'
6
6
import CP from 'childprocess-promise'
7
-
8
- export const bundledEslintPath = Path . join ( FS . realpathSync ( Path . join ( __dirname , '..' ) ) , 'node_modules' , 'eslint' )
7
+ import { findFile } from 'atom-linter'
9
8
10
9
export function spawnWorker ( ) {
11
10
let shouldLive = true
@@ -39,7 +38,7 @@ export function spawnWorker() {
39
38
} } }
40
39
}
41
40
42
- export function getCliFromPath ( path ) {
41
+ export function getCliFromDirectory ( path ) {
43
42
try {
44
43
return require ( Path . join ( path , 'lib' , 'cli.js' ) )
45
44
} catch ( e ) {
@@ -48,3 +47,46 @@ export function getCliFromPath(path) {
48
47
} else throw e
49
48
}
50
49
}
50
+
51
+ let nodePrefixPath = null
52
+
53
+ export function getNodePrefixPath ( ) {
54
+ if ( nodePrefixPath === null ) {
55
+ const npmCommand = process . platform === 'win32' ? 'npm.cmd' : 'npm'
56
+ try {
57
+ nodePrefixPath = ChildProcess . spawnSync ( npmCommand , [ 'get' , 'prefix' ] ) . output [ 1 ] . toString ( ) . trim ( )
58
+ } catch ( e ) {
59
+ throw new Error ( 'Unable to execute `npm get prefix`. Please make sure Atom is getting $PATH correctly' )
60
+ }
61
+ }
62
+ return nodePrefixPath
63
+ }
64
+
65
+ let bundledEslintDirectory = null
66
+
67
+ export function getBundledEslintDirectory ( ) {
68
+ if ( bundledEslintDirectory === null ) {
69
+ bundledEslintDirectory = Path . join ( FS . realpathSync ( Path . join ( __dirname , '..' ) ) , 'node_modules' , 'eslint' )
70
+ }
71
+ return bundledEslintDirectory
72
+ }
73
+
74
+ export function getEslintDirectory ( params ) {
75
+ if ( params . global ) {
76
+ const prefixPath = getNodePrefixPath ( )
77
+ if ( process . platform === 'win32' ) {
78
+ return Path . join ( params . nodePath || prefixPath , 'node_modules' , 'eslint' )
79
+ } else {
80
+ return Path . join ( params . nodePath || prefixPath , 'lib' , 'node_modules' , 'eslint' )
81
+ }
82
+ } else {
83
+ const modulesPath = findFile ( params . fileDir , 'node_modules' )
84
+ const eslintPath = Path . join ( modulesPath , 'eslint' )
85
+ try {
86
+ FS . accessSync ( eslintPath , FS . R_OK )
87
+ return eslintPath
88
+ } catch ( _ ) {
89
+ return getBundledEslintDirectory ( )
90
+ }
91
+ }
92
+ }
0 commit comments