-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcli.js
executable file
·88 lines (71 loc) · 2.25 KB
/
cli.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
#!/usr/bin/env node
const { spawnSync } = require('child_process')
const fs = require('fs')
const { dirname, join } = require('path')
const randomChars = () => {
return Math.random().toString(36).slice(2)
}
const resolveFromModule = (moduleName, ...paths) => {
const modulePath = dirname(require.resolve(`${moduleName}/package.json`))
return join(modulePath, ...paths)
}
const resolveFromRoot = (...paths) => {
return join(process.cwd(), ...paths)
}
const args = process.argv.slice(2)
const argsProjectIndex = args.findIndex(arg =>
['-p', '--project'].includes(arg),
)
const argsProjectValue =
argsProjectIndex !== -1 ? args[argsProjectIndex + 1] : undefined
const files = args.filter(file => /\.(ts|tsx)$/.test(file))
if (files.length === 0) {
process.exit(0)
}
const remainingArgsToForward = args.slice().filter(arg => !files.includes(arg))
if (argsProjectIndex !== -1) {
remainingArgsToForward.splice(argsProjectIndex, 2)
}
// Load existing config
const tsconfigPath = argsProjectValue || resolveFromRoot('tsconfig.json')
const tsconfigContent = fs.readFileSync(tsconfigPath).toString()
// Use 'eval' to read the JSON as regular JavaScript syntax so that comments are allowed
let tsconfig = {}
eval(`tsconfig = ${tsconfigContent}`)
// Write a temp config file
const tmpTsconfigPath = resolveFromRoot(`tsconfig.${randomChars()}.json`)
const tmpTsconfig = {
...tsconfig,
compilerOptions: {
...tsconfig.compilerOptions,
skipLibCheck: true,
},
files,
include: [],
}
fs.writeFileSync(tmpTsconfigPath, JSON.stringify(tmpTsconfig, null, 2))
// Attach cleanup handlers
let didCleanup = false
for (const eventName of ['exit', 'SIGHUP', 'SIGINT', 'SIGTERM']) {
process.on(eventName, exitCode => {
if (didCleanup) return
didCleanup = true
fs.unlinkSync(tmpTsconfigPath)
if (eventName !== 'exit') {
process.exit(exitCode)
}
})
}
// Type-check our files
const { status } = spawnSync(
// See: https://github.com/gustavopch/tsc-files/issues/44#issuecomment-1250783206
process.versions.pnp
? 'tsc'
: resolveFromModule(
'typescript',
`../.bin/tsc${process.platform === 'win32' ? '.cmd' : ''}`,
),
['-p', tmpTsconfigPath, ...remainingArgsToForward],
{ stdio: 'inherit' },
)
process.exit(status)