@@ -19,12 +19,13 @@ const EVAL_FILENAME = `[eval].ts`
1919/**
2020 * Eval state management.
2121 */
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 ) { }
2829}
2930
3031/**
@@ -82,6 +83,9 @@ export function main (argv: string[]) {
8283 '--help' : help = false ,
8384 '--script-mode' : scriptMode = false ,
8485 '--version' : version = 0 ,
86+ '--require' : requires = [ ] ,
87+ '--eval' : code = undefined ,
88+ '--print' : print = false ,
8589 '--files' : files = DEFAULTS . files ,
8690 '--compiler' : compiler = DEFAULTS . compiler ,
8791 '--compiler-options' : compilerOptions = DEFAULTS . compilerOptions ,
@@ -135,10 +139,8 @@ export function main (argv: string[]) {
135139 process . exit ( 0 )
136140 }
137141
138- const code = args [ '--eval' ]
139- const isPrinted = args [ '--print' ] !== undefined
140142 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 ) )
142144
143145 // Register the TypeScript compiler instance.
144146 const service = register ( {
@@ -186,16 +188,21 @@ export function main (argv: string[]) {
186188 process . exit ( 0 )
187189 }
188190
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+
189196 // 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 )
191198
192199 // Prepend `ts-node` arguments to CLI for child processes.
193200 process . execArgv . unshift ( __filename , ...process . argv . slice ( 2 , process . argv . length - args . _ . length ) )
194201 process . argv = [ process . argv [ 1 ] ] . concat ( scriptPath || [ ] ) . concat ( args . _ . slice ( 1 ) )
195202
196203 // Execute the main contents (either eval, script or piped).
197204 if ( code !== undefined && ! interactive ) {
198- evalAndExit ( service , state , cwd , code , isPrinted )
205+ evalAndExit ( service , state , module , code , print )
199206 } else {
200207 if ( args . _ . length ) {
201208 Module . runMain ( )
@@ -206,7 +213,7 @@ export function main (argv: string[]) {
206213 } else {
207214 let buffer = code || ''
208215 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 ) )
210217 }
211218 }
212219 }
@@ -231,19 +238,15 @@ function getCwd (cwd: string, scriptMode?: boolean, scriptPath?: string) {
231238/**
232239 * Evaluate a script.
233240 */
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
238243
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 )
241246 ; ( global as any ) . exports = module . exports
242247 ; ( global as any ) . module = module
243248 ; ( global as any ) . require = module . require . bind ( module )
244249
245- let result : any
246-
247250 try {
248251 result = _eval ( service , state , code )
249252 } catch ( error ) {
@@ -286,7 +289,7 @@ function _eval (service: Register, state: EvalState, input: string) {
286289 }
287290
288291 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
290293 } , undefined )
291294}
292295
@@ -360,7 +363,7 @@ function startRepl (service: Register, state: EvalState, code?: string) {
360363 resetEval ( )
361364
362365 // Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`.
363- exec ( 'exports = module.exports' , EVAL_FILENAME )
366+ exec ( 'exports = module.exports' , state . path )
364367 }
365368
366369 reset ( )
0 commit comments