-
Notifications
You must be signed in to change notification settings - Fork 51
/
github-lint.js
executable file
·67 lines (52 loc) · 1.72 KB
/
github-lint.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
#!/usr/bin/env node
// usage: github-lint
//
// Run ESLint and Flow on project.
const childProcess = require('child_process')
const fs = require('fs')
const path = require('path')
const supportsColors = require('supports-color')
const hasBasicColorSupport = supportsColors.stdout.hasBasic && supportsColors.stderr.hasBasic
function execFile(command, args) {
return new Promise(resolve => {
childProcess.execFile(command, args, {maxBuffer: 1024 ** 2}, (error, stdout, stderr) => {
resolve({code: error ? error.code : 0, stdout, stderr})
})
})
}
;(async function() {
let runs = 0
const codes = []
const commands = []
const packageJson = fs.existsSync('package.json') ? require(path.join(process.cwd(), 'package.json')) : {}
let eslintOptions = ['--report-unused-disable-directives', '.']
if (hasBasicColorSupport) {
eslintOptions = eslintOptions.concat(['--color'])
}
const isTypeScriptProject = fs.existsSync('tsconfig.json')
if (isTypeScriptProject) {
eslintOptions = eslintOptions.concat(['--ext', '.js,.ts,.tsx'])
}
commands.push(['eslint', eslintOptions])
if (isTypeScriptProject) {
commands.push(['tsc', ['--noEmit']])
}
for (const [command, args] of commands) {
if (runs > 0) process.stderr.write('\n')
process.stderr.write(`> ${command} ${args.join(' ')}\n`)
const {code, stdout, stderr} = await execFile(command, args)
codes.push(code)
if (stderr) process.stderr.write(stderr)
if (stdout) process.stdout.write(stdout)
runs++
}
const nonzero = codes.find(code => code !== 0)
if (nonzero) {
process.stderr.write(`\nCommand failed: ${nonzero}\n`)
process.exit(nonzero)
}
})().catch(error => {
setTimeout(() => {
throw error
})
})