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

Commit 7bba1a2

Browse files
author
steelbrain
committed
🆕 Port more helpers to babel
1 parent a25dfbd commit 7bba1a2

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/helpers.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import Path from 'path'
44
import FS from 'fs'
55
import ChildProcess from 'child_process'
66
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'
98

109
export function spawnWorker() {
1110
let shouldLive = true
@@ -39,7 +38,7 @@ export function spawnWorker() {
3938
}}}
4039
}
4140

42-
export function getCliFromPath(path) {
41+
export function getCliFromDirectory(path) {
4342
try {
4443
return require(Path.join(path, 'lib', 'cli.js'))
4544
} catch (e) {
@@ -48,3 +47,46 @@ export function getCliFromPath(path) {
4847
} else throw e
4948
}
5049
}
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

Comments
 (0)