-
-
Notifications
You must be signed in to change notification settings - Fork 533
/
bin.ts
486 lines (417 loc) · 12.7 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/usr/bin/env node
import { join, resolve, dirname } from 'path'
import { start, Recoverable } from 'repl'
import { inspect } from 'util'
import Module = require('module')
import arg = require('arg')
import { diffLines } from 'diff'
import { Script } from 'vm'
import { readFileSync, statSync } from 'fs'
import { homedir } from 'os'
import { VERSION, TSError, parse, Register, register } from './index'
/**
* Eval filename for REPL/debug.
*/
const EVAL_FILENAME = `[eval].ts`
/**
* Eval state management.
*/
class EvalState {
input = ''
output = ''
version = 0
lines = 0
constructor (public path: string) {}
}
/**
* Main `bin` functionality.
*/
export function main (argv: string[]) {
const args = arg({
// Node.js-like options.
'--eval': String,
'--interactive': Boolean,
'--print': Boolean,
'--require': [String],
// CLI options.
'--help': Boolean,
'--script-mode': Boolean,
'--version': arg.COUNT,
// Project options.
'--dir': String,
'--files': Boolean,
'--compiler': String,
'--compiler-options': parse,
'--project': String,
'--ignore-diagnostics': [String],
'--ignore': [String],
'--transpile-only': Boolean,
'--type-check': Boolean,
'--compiler-host': Boolean,
'--pretty': Boolean,
'--skip-project': Boolean,
'--skip-ignore': Boolean,
'--prefer-ts-exts': Boolean,
'--log-error': Boolean,
'--emit': Boolean,
// Aliases.
'-e': '--eval',
'-i': '--interactive',
'-p': '--print',
'-r': '--require',
'-h': '--help',
'-s': '--script-mode',
'-v': '--version',
'-T': '--transpile-only',
'-H': '--compiler-host',
'-I': '--ignore',
'-P': '--project',
'-C': '--compiler',
'-D': '--ignore-diagnostics',
'-O': '--compiler-options'
}, {
argv,
stopAtPositional: true
})
// Only setting defaults for CLI-specific flags
// Anything passed to `register()` can be `undefined`; `create()` will apply
// defaults.
const {
'--dir': dir,
'--help': help = false,
'--script-mode': scriptMode = false,
'--version': version = 0,
'--require': requires = [],
'--eval': code = undefined,
'--print': print = false,
'--interactive': interactive = false,
'--files': files,
'--compiler': compiler,
'--compiler-options': compilerOptions,
'--project': project,
'--ignore-diagnostics': ignoreDiagnostics,
'--ignore': ignore,
'--transpile-only': transpileOnly,
'--type-check': typeCheck,
'--compiler-host': compilerHost,
'--pretty': pretty,
'--skip-project': skipProject,
'--skip-ignore': skipIgnore,
'--prefer-ts-exts': preferTsExts,
'--log-error': logError,
'--emit': emit
} = args
if (help) {
console.log(`
Usage: ts-node [options] [ -e script | script.ts ] [arguments]
Options:
-e, --eval [code] Evaluate code
-p, --print Print result of \`--eval\`
-r, --require [path] Require a node module before execution
-i, --interactive Opens the REPL even if stdin does not appear to be a terminal
-h, --help Print CLI usage
-v, --version Print module version information
-s, --script-mode Use cwd from <script.ts> instead of current directory
-T, --transpile-only Use TypeScript's faster \`transpileModule\`
-H, --compiler-host Use TypeScript's compiler host API
-I, --ignore [pattern] Override the path patterns to skip compilation
-P, --project [path] Path to TypeScript JSON project file
-C, --compiler [name] Specify a custom TypeScript compiler
-D, --ignore-diagnostics [code] Ignore TypeScript warnings by diagnostic code
-O, --compiler-options [opts] JSON object to merge with compiler options
--dir Specify working directory for config resolution
--scope Scope compiler to files within \`cwd\` only
--files Load \`files\`, \`include\` and \`exclude\` from \`tsconfig.json\` on startup
--pretty Use pretty diagnostic formatter (usually enabled by default)
--skip-project Skip reading \`tsconfig.json\`
--skip-ignore Skip \`--ignore\` checks
--prefer-ts-exts Prefer importing TypeScript files over JavaScript files
--log-error Logs TypeScript errors to stderr instead of throwing exceptions
`)
process.exit(0)
}
// Output project information.
if (version === 1) {
console.log(`v${VERSION}`)
process.exit(0)
}
const cwd = dir || process.cwd()
const scriptPath = args._.length ? resolve(cwd, args._[0]) : undefined
const state = new EvalState(scriptPath || join(cwd, EVAL_FILENAME))
// Register the TypeScript compiler instance.
const service = register({
dir: getCwd(dir, scriptMode, scriptPath),
emit,
files,
pretty,
transpileOnly,
typeCheck,
compilerHost,
ignore,
preferTsExts,
logError,
project,
skipProject,
skipIgnore,
compiler,
ignoreDiagnostics,
compilerOptions,
readFile: code !== undefined
? (path: string) => {
if (path === state.path) return state.input
try {
return readFileSync(path, 'utf8')
} catch (err) {/* Ignore. */}
}
: undefined,
fileExists: code !== undefined
? (path: string) => {
if (path === state.path) return true
try {
const stats = statSync(path)
return stats.isFile() || stats.isFIFO()
} catch (err) {
return false
}
}
: undefined
})
// Output project information.
if (version >= 2) {
console.log(`ts-node v${VERSION}`)
console.log(`node ${process.version}`)
console.log(`compiler v${service.ts.version}`)
process.exit(0)
}
// Create a local module instance based on `cwd`.
const module = new Module(state.path)
module.filename = state.path
module.paths = (Module as any)._nodeModulePaths(cwd)
// Require specified modules before start-up.
;(Module as any)._preloadModules(requires)
// Prepend `ts-node` arguments to CLI for child processes.
process.execArgv.unshift(__filename, ...process.argv.slice(2, process.argv.length - args._.length))
process.argv = [process.argv[1]].concat(scriptPath || []).concat(args._.slice(1))
// Execute the main contents (either eval, script or piped).
if (code !== undefined && !interactive) {
evalAndExit(service, state, module, code, print)
} else {
if (args._.length) {
Module.runMain()
} else {
// Piping of execution _only_ occurs when no other script is specified.
if (process.stdin.isTTY) {
startRepl(service, state, code)
} else {
let buffer = code || ''
process.stdin.on('data', (chunk: Buffer) => buffer += chunk)
process.stdin.on('end', () => evalAndExit(service, state, module, buffer, print))
}
}
}
}
/**
* Get project path from args.
*/
function getCwd (dir?: string, scriptMode?: boolean, scriptPath?: string) {
// Validate `--script-mode` usage is correct.
if (scriptMode) {
if (!scriptPath) {
throw new TypeError('Script mode must be used with a script name, e.g. `ts-node -s <script.ts>`')
}
if (dir) {
throw new TypeError('Script mode cannot be combined with `--dir`')
}
return dirname(scriptPath)
}
return dir
}
/**
* Evaluate a script.
*/
function evalAndExit (service: Register, state: EvalState, module: Module, code: string, isPrinted: boolean) {
let result: any
;(global as any).__filename = module.filename
;(global as any).__dirname = dirname(module.filename)
;(global as any).exports = module.exports
;(global as any).module = module
;(global as any).require = module.require.bind(module)
try {
result = _eval(service, state, code)
} catch (error) {
if (error instanceof TSError) {
console.error(error)
process.exit(1)
}
throw error
}
if (isPrinted) {
console.log(typeof result === 'string' ? result : inspect(result))
}
}
/**
* Evaluate the code snippet.
*/
function _eval (service: Register, state: EvalState, input: string) {
const lines = state.lines
const isCompletion = !/\n$/.test(input)
const undo = appendEval(state, input)
let output: string
try {
output = service.compile(state.input, state.path, -lines)
} catch (err) {
undo()
throw err
}
// Use `diff` to check for new JavaScript to execute.
const changes = diffLines(state.output, output)
if (isCompletion) {
undo()
} else {
state.output = output
}
return changes.reduce((result, change) => {
return change.added ? exec(change.value, state.path) : result
}, undefined)
}
/**
* Execute some code.
*/
function exec (code: string, filename: string) {
const script = new Script(code, { filename: filename })
return script.runInThisContext()
}
/**
* Start a CLI REPL.
*/
function startRepl (service: Register, state: EvalState, code?: string) {
// Eval incoming code before the REPL starts.
if (code) {
try {
_eval(service, state, `${code}\n`)
} catch (err) {
console.error(err)
process.exit(1)
}
}
const repl = start({
prompt: '> ',
input: process.stdin,
output: process.stdout,
terminal: process.stdout.isTTY,
eval: replEval,
useGlobal: true
})
/**
* Eval code from the REPL.
*/
function replEval (code: string, _context: any, _filename: string, callback: (err: Error | null, result?: any) => any) {
let err: Error | null = null
let result: any
// TODO: Figure out how to handle completion here.
if (code === '.scope') {
callback(err)
return
}
try {
result = _eval(service, state, code)
} catch (error) {
if (error instanceof TSError) {
// Support recoverable compilations using >= node 6.
if (Recoverable && isRecoverable(error)) {
err = new Recoverable(error)
} else {
console.error(error)
}
} else {
err = error
}
}
return callback(err, result)
}
// Bookmark the point where we should reset the REPL state.
const resetEval = appendEval(state, '')
function reset () {
resetEval()
// Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`.
exec('exports = module.exports', state.path)
}
reset()
repl.on('reset', reset)
repl.defineCommand('type', {
help: 'Check the type of a TypeScript identifier',
action: function (identifier: string) {
if (!identifier) {
repl.displayPrompt()
return
}
const undo = appendEval(state, identifier)
const { name, comment } = service.getTypeInfo(state.input, state.path, state.input.length)
undo()
if (name) repl.outputStream.write(`${name}\n`)
if (comment) repl.outputStream.write(`${comment}\n`)
repl.displayPrompt()
}
})
// Set up REPL history when available natively via node.js >= 11.
if (repl.setupHistory) {
const historyPath = process.env.TS_NODE_HISTORY || join(homedir(), '.ts_node_repl_history')
repl.setupHistory(historyPath, err => {
if (!err) return
console.error(err)
process.exit(1)
})
}
}
/**
* Append to the eval instance and return an undo function.
*/
function appendEval (state: EvalState, input: string) {
const undoInput = state.input
const undoVersion = state.version
const undoOutput = state.output
const undoLines = state.lines
// Handle ASI issues with TypeScript re-evaluation.
if (undoInput.charAt(undoInput.length - 1) === '\n' && /^\s*[\/\[(`-]/.test(input) && !/;\s*$/.test(undoInput)) {
state.input = `${state.input.slice(0, -1)};\n`
}
state.input += input
state.lines += lineCount(input)
state.version++
return function () {
state.input = undoInput
state.output = undoOutput
state.version = undoVersion
state.lines = undoLines
}
}
/**
* Count the number of lines.
*/
function lineCount (value: string) {
let count = 0
for (const char of value) {
if (char === '\n') {
count++
}
}
return count
}
const RECOVERY_CODES: Set<number> = new Set([
1003, // "Identifier expected."
1005, // "')' expected."
1109, // "Expression expected."
1126, // "Unexpected end of text."
1160, // "Unterminated template literal."
1161, // "Unterminated regular expression literal."
2355 // "A function whose declared type is neither 'void' nor 'any' must return a value."
])
/**
* Check if a function can recover gracefully.
*/
function isRecoverable (error: TSError) {
return error.diagnosticCodes.every(code => RECOVERY_CODES.has(code))
}
if (require.main === module) {
main(process.argv.slice(2))
}