Skip to content

Commit

Permalink
Replace internal _preloadModules usage
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 8, 2019
1 parent 1ad44bf commit 2e99c50
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ const EVAL_FILENAME = `[eval].ts`
/**
* Eval state management.
*/
interface EvalState {
path: string
input: string
output: string
version: number
lines: number
class EvalState {
input = ''
output = ''
version = 0
lines = 0

constructor (public path: string) {}
}

/**
Expand Down Expand Up @@ -82,6 +83,9 @@ export function main (argv: string[]) {
'--help': help = false,
'--script-mode': scriptMode = false,
'--version': version = 0,
'--require': requires = [],
'--eval': code = undefined,
'--print': print = false,
'--files': files = DEFAULTS.files,
'--compiler': compiler = DEFAULTS.compiler,
'--compiler-options': compilerOptions = DEFAULTS.compilerOptions,
Expand Down Expand Up @@ -135,10 +139,8 @@ export function main (argv: string[]) {
process.exit(0)
}

const code = args['--eval']
const isPrinted = args['--print'] !== undefined
const scriptPath = args._.length ? resolve(cwd, args._[0]) : undefined
const state = { path: join(cwd, EVAL_FILENAME), input: '', output: '', version: 0, lines: 0 }
const state = new EvalState(scriptPath || join(cwd, EVAL_FILENAME))

// Register the TypeScript compiler instance.
const service = register({
Expand Down Expand Up @@ -186,16 +188,21 @@ export function main (argv: string[]) {
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.
if (args['--require']) (Module as any)._preloadModules(args['--require'])
for (const id of requires) module.require(id)

// 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, cwd, code, isPrinted)
evalAndExit(service, state, module, code, print)
} else {
if (args._.length) {
Module.runMain()
Expand All @@ -206,7 +213,7 @@ export function main (argv: string[]) {
} else {
let buffer = code || ''
process.stdin.on('data', (chunk: Buffer) => buffer += chunk)
process.stdin.on('end', () => evalAndExit(service, state, cwd, buffer, isPrinted))
process.stdin.on('end', () => evalAndExit(service, state, module, buffer, print))
}
}
}
Expand All @@ -231,19 +238,15 @@ function getCwd (cwd: string, scriptMode?: boolean, scriptPath?: string) {
/**
* Evaluate a script.
*/
function evalAndExit (service: Register, state: EvalState, cwd: string, code: string, isPrinted: boolean) {
const module = new Module(EVAL_FILENAME)
module.filename = EVAL_FILENAME
module.paths = (Module as any)._nodeModulePaths(cwd)
function evalAndExit (service: Register, state: EvalState, module: Module, code: string, isPrinted: boolean) {
let result: any

;(global as any).__filename = EVAL_FILENAME
;(global as any).__dirname = cwd
;(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)

let result: any

try {
result = _eval(service, state, code)
} catch (error) {
Expand Down Expand Up @@ -286,7 +289,7 @@ function _eval (service: Register, state: EvalState, input: string) {
}

return changes.reduce((result, change) => {
return change.added ? exec(change.value, EVAL_FILENAME) : result
return change.added ? exec(change.value, state.path) : result
}, undefined)
}

Expand Down Expand Up @@ -360,7 +363,7 @@ function startRepl (service: Register, state: EvalState, code?: string) {
resetEval()

// Hard fix for TypeScript forcing `Object.defineProperty(exports, ...)`.
exec('exports = module.exports', EVAL_FILENAME)
exec('exports = module.exports', state.path)
}

reset()
Expand Down

0 comments on commit 2e99c50

Please sign in to comment.