-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathbin.ts
187 lines (169 loc) · 3.47 KB
/
bin.ts
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
import { runDev } from '.'
import minimist from 'minimist'
const nodeArgs: string[] = []
const unknown: string[] = []
const devArgs = process.argv.slice(2, 100)
const tsNodeFlags = {
boolean: [
'scope',
'emit',
'files',
'pretty',
'transpile-only',
'swc',
'prefer-ts-exts',
'prefer-ts',
'log-error',
'skip-project',
'skip-ignore',
'compiler-host',
'script-mode',
],
string: [
'compiler',
'project',
'ignore',
'ignore-diagnostics',
'compiler-options',
'scopeDir',
'transpiler'
],
}
const tsNodeAlias = {
'transpile-only': 'T',
'swc': 'W',
'compiler-host': 'H',
ignore: 'I',
'ignore-diagnostics': 'D',
'compiler-options': 'O',
compiler: 'C',
project: 'P',
'script-mode': 's',
}
type TSNodeOptions = {
project: string
compilerOptions: any
'compiler-options': any
'prefer-ts-exts': boolean
ignore?: string
dir: string
'script-mode': boolean
emit: boolean
files: boolean
'transpile-only': boolean
swc: boolean
pretty: boolean
scope: boolean
scopeDir: string,
transpiler: string
'log-error': boolean
'skip-project': boolean
'skip-ignore': boolean
compiler: string
'compiler-host': boolean
'ignore-diagnostics': string
}
const devFlags = {
boolean: [
'deps',
'all-deps',
'dedupe',
'fork',
'exec-check',
'debug',
'poll',
'respawn',
'notify',
'tree-kill',
'clear',
'cls',
'exit-child',
'error-recompile',
'quiet',
'rs',
],
string: [
'dir',
'deps-level',
'compile-timeout',
'ignore-watch',
'interval',
'debounce',
'watch',
'cache-directory',
],
}
type DevOptions = {
poll: boolean
debug: boolean
fork: boolean
watch: string
interval: string
rs: boolean
deps: boolean
dedupe: boolean
respawn: boolean
notify: boolean
clear: boolean
cls: boolean
'ignore-watch': string
'all-deps': boolean
'deps-level': string
'compile-timeout': string
'exec-check': boolean
'exit-child': boolean
'cache-directory': string
'error-recompile': boolean
quiet: boolean
'tree-kill': boolean
}
export type Options = {
priorNodeArgs: string[]
_: string[]
} & DevOptions &
TSNodeOptions
const opts = minimist(devArgs, {
stopEarly: true,
boolean: [...devFlags.boolean, ...tsNodeFlags.boolean],
string: [...devFlags.string, ...tsNodeFlags.string],
alias: {
...tsNodeAlias,
'prefer-ts-exts': 'prefer-ts',
},
default: {
fork: true,
},
unknown: function (arg) {
unknown.push(arg)
return true
},
}) as Options
const script = opts._[0]
const scriptArgs = opts._.slice(1)
opts.priorNodeArgs = []
unknown.forEach(function (arg) {
if (arg === script || nodeArgs.indexOf(arg) >= 0) return
const argName = arg.replace(/^-+/, '')
// fix this
const argOpts = (opts as any)[argName]
const argValues = Array.isArray(argOpts) ? argOpts : [argOpts]
argValues.forEach(function (argValue) {
if ((arg === '-r' || arg === '--require') && argValue === 'esm') {
opts.priorNodeArgs.push(arg, argValue)
return false
}
nodeArgs.push(arg)
if (typeof argValue === 'string') {
nodeArgs.push(argValue)
}
})
})
if (!script) {
// eslint-disable-next-line no-console
console.log('ts-node-dev: no script to run provided')
// eslint-disable-next-line no-console
console.log('Usage: ts-node-dev [options] script [arguments]\n')
process.exit(1)
}
runDev(script, scriptArgs, nodeArgs, opts)