-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathchecker.js
98 lines (84 loc) · 2.61 KB
/
checker.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env node
/* eslint-disable no-console */
const program = require('commander')
const chalk = require('chalk')
const { resolve } = require('path')
const { writeFileSync, realpathSync } = require('fs')
const mkdirp = require('mkdirp')
const { version } = require('./package.json')
// https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/config/paths.js#L17
const appDirectory = realpathSync(process.cwd())
const resolveAppPath = relativePath => resolve(appDirectory, relativePath)
const defaultOptions = {
file: 'results.txt',
errorFile: 'error.txt',
}
module.exports = () => {
const debug = (...str) => console.log(chalk.yellow(...str))
program
.version(version)
.option('--no-color')
.option('--debugging')
.option(
'-d, --dir <dir>',
'The directory where to save the flow result and errors. If not specified no files will be saved.'
)
.option(
'-f, --file <filename>',
`Name of the result file. defaults to '${defaultOptions.file}'`,
defaultOptions.file
)
.option(
'-e, --error-file <filename>',
`Name of the file where parsing errors are stored. defaults to '${defaultOptions.errorFile}'`,
defaultOptions.errorFile
)
.parse(process.argv)
const { file, errorFile, dir = '', debugging } = program
if (debugging) {
debug('file ', file)
debug('errorFile ', errorFile)
debug('dir ', dir)
}
const dirPath = dir.indexOf('/') !== 0 ? resolveAppPath(dir) : resolve(dir)
return ({ stdout, stderr }) => {
try {
if (dir) {
mkdirp.sync(dirPath)
}
if (stderr) {
console.error(chalk.bold.red(stderr))
process.exit(1)
}
if (stdout) {
if (dir) {
writeFileSync(resolve(dirPath, file), stdout)
}
if (debugging) {
debug(stdout)
}
const result = stdout
.replace(/\r/g, '')
.replace(/\n*Found.*errors?\n/, '')
.replace(/\n*No errors!\n/, '')
.split(/\n\n/)
.filter(line => line && !line.includes('Error: node_modules') && !line.includes('[No file]'))
if (result.length > 0) {
console.log(`
${chalk.red(result.join('\n\n'))}
${chalk.bold.red(`Found ${result.length} error${result.length === 1 ? '' : 's'}`)}
`)
process.exit(1)
} else {
console.log(chalk.bold.green('\nFound 0 errors\n'))
}
}
} catch (e) {
console.error(chalk.bold.red(e.stack))
if (dir) {
writeFileSync(resolve(dirPath, errorFile), e.stack)
}
process.exit(1)
}
}
}