-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·71 lines (56 loc) · 1.89 KB
/
index.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
#!/usr/bin/env node
const findPackageJson = require('find-package-json')
const childProcess = require('child_process')
const path = require('path')
const debug = require('debug')('common-lint')
const args = process.argv.slice(2)
const startLocation = args.reduce((acc, cur) => {
const startsWithDash = cur.startsWith('-')
if (startsWithDash === false) {
acc = cur
}
return acc
}, process.cwd())
// It's important that we start looking in the process CWD.
// We don't use `__dirname` here because it's the path to this file.
const finder = findPackageJson(startLocation)
// Find the closest `package.json` and extract dependencies.
const packageFile = finder.next()
const hasPackageFile = packageFile.value != null
const command = 'eslint'
let config = null
if (hasPackageFile) {
debug('has package file')
const packageFilePath = packageFile.filename
const modulePath = path.dirname(packageFilePath)
debug({ modulePath })
const { dependencies, devDependencies } = packageFile.value
const allDependencies = [dependencies, devDependencies]
const hasStandard = allDependencies.some((deps = {}) =>
Object.keys(deps).includes('standard')
)
if (hasStandard) {
config = path.join(modulePath, 'node_modules', 'standard', 'eslintrc.json')
}
}
const notFoundError = `
Could not locate "${command}", is it installed?
- npm -g install ${command}
- yarn global add ${command}
Please ensure that "${command} --help" works and then try again.
`
const prefix = config === null ? '' : ['--config', config]
try {
const commandArgs = [...prefix, ...args]
debug({ prefix, args, commandArgs })
childProcess.execFileSync(command, commandArgs, {
stdio: 'inherit'
})
} catch (err) {
// We can safely ignore most errors, but we *do* want to catch "not found"
// errors and output them to the user.
if (err.code === 'ENOENT') {
console.error(notFoundError)
process.exit(127)
}
}