@@ -19,12 +19,13 @@ const EVAL_FILENAME = `[eval].ts`
19
19
/**
20
20
* Eval state management.
21
21
*/
22
- interface EvalState {
23
- path: string
24
- input: string
25
- output: string
26
- version: number
27
- lines: number
22
+ class EvalState {
23
+ input = ''
24
+ output = ''
25
+ version = 0
26
+ lines = 0
27
+
28
+ constructor ( public path : string ) { }
28
29
}
29
30
30
31
/**
@@ -82,6 +83,9 @@ export function main (argv: string[]) {
82
83
'--help' : help = false ,
83
84
'--script-mode' : scriptMode = false ,
84
85
'--version' : version = 0 ,
86
+ '--require' : requires = [ ] ,
87
+ '--eval' : code = undefined ,
88
+ '--print' : print = false ,
85
89
'--files' : files = DEFAULTS . files ,
86
90
'--compiler' : compiler = DEFAULTS . compiler ,
87
91
'--compiler-options' : compilerOptions = DEFAULTS . compilerOptions ,
@@ -135,10 +139,8 @@ export function main (argv: string[]) {
135
139
process . exit ( 0 )
136
140
}
137
141
138
- const code = args [ '--eval' ]
139
- const isPrinted = args [ '--print' ] !== undefined
140
142
const scriptPath = args . _ . length ? resolve ( cwd , args . _ [ 0 ] ) : undefined
141
- const state = { path : join ( cwd , EVAL_FILENAME ) , input : '' , output : '' , version : 0 , lines : 0 }
143
+ const state = new EvalState ( scriptPath || join ( cwd , EVAL_FILENAME ) )
142
144
143
145
// Register the TypeScript compiler instance.
144
146
const service = register ( {
@@ -186,16 +188,21 @@ export function main (argv: string[]) {
186
188
process . exit ( 0 )
187
189
}
188
190
191
+ // Create a local module instance based on `cwd`.
192
+ const module = new Module ( state . path )
193
+ module . filename = state . path
194
+ module . paths = ( Module as any ) . _nodeModulePaths ( cwd )
195
+
189
196
// Require specified modules before start-up.
190
- if ( args [ '--require' ] ) ( Module as any ) . _preloadModules ( args [ '-- require' ] )
197
+ for ( const id of requires ) module . require ( id )
191
198
192
199
// Prepend `ts-node` arguments to CLI for child processes.
193
200
process . execArgv . unshift ( __filename , ...process . argv . slice ( 2 , process . argv . length - args . _ . length ) )
194
201
process . argv = [ process . argv [ 1 ] ] . concat ( scriptPath || [ ] ) . concat ( args . _ . slice ( 1 ) )
195
202
196
203
// Execute the main contents (either eval, script or piped).
197
204
if ( code !== undefined && ! interactive ) {
198
- evalAndExit ( service , state , cwd , code , isPrinted )
205
+ evalAndExit ( service , state , module , code , print )
199
206
} else {
200
207
if ( args . _ . length ) {
201
208
Module . runMain ( )
@@ -206,7 +213,7 @@ export function main (argv: string[]) {
206
213
} else {
207
214
let buffer = code || ''
208
215
process . stdin . on ( 'data' , ( chunk : Buffer ) => buffer += chunk )
209
- process . stdin . on ( 'end' , ( ) => evalAndExit ( service , state , cwd , buffer , isPrinted ) )
216
+ process . stdin . on ( 'end' , ( ) => evalAndExit ( service , state , module , buffer , print ) )
210
217
}
211
218
}
212
219
}
@@ -231,19 +238,15 @@ function getCwd (cwd: string, scriptMode?: boolean, scriptPath?: string) {
231
238
/**
232
239
* Evaluate a script.
233
240
*/
234
- function evalAndExit ( service : Register , state : EvalState , cwd : string , code : string , isPrinted : boolean ) {
235
- const module = new Module ( EVAL_FILENAME )
236
- module . filename = EVAL_FILENAME
237
- module . paths = ( Module as any ) . _nodeModulePaths ( cwd )
241
+ function evalAndExit ( service : Register , state : EvalState , module : Module , code : string , isPrinted : boolean ) {
242
+ let result : any
238
243
239
- ; ( global as any ) . __filename = EVAL_FILENAME
240
- ; ( global as any ) . __dirname = cwd
244
+ ; ( global as any ) . __filename = module . filename
245
+ ; ( global as any ) . __dirname = dirname ( module . filename )
241
246
; ( global as any ) . exports = module . exports
242
247
; ( global as any ) . module = module
243
248
; ( global as any ) . require = module . require . bind ( module )
244
249
245
- let result : any
246
-
247
250
try {
248
251
result = _eval ( service , state , code )
249
252
} catch ( error ) {
@@ -286,7 +289,7 @@ function _eval (service: Register, state: EvalState, input: string) {
286
289
}
287
290
288
291
return changes . reduce ( ( result , change ) => {
289
- return change . added ? exec ( change . value , EVAL_FILENAME ) : result
292
+ return change . added ? exec ( change . value , state . path ) : result
290
293
} , undefined )
291
294
}
292
295
@@ -360,7 +363,7 @@ function startRepl (service: Register, state: EvalState, code?: string) {
360
363
resetEval ( )
361
364
362
365
// Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`.
363
- exec ( 'exports = module.exports' , EVAL_FILENAME )
366
+ exec ( 'exports = module.exports' , state . path )
364
367
}
365
368
366
369
reset ( )
0 commit comments