From 022ce29d2dc8929b4bb0827de5a7e035f90f098e Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 21 Jan 2019 16:09:56 -0800 Subject: [PATCH] Preserve `ts-node` `execArgv` for spawning process --- src/bin.ts | 6 ++++-- src/index.spec.ts | 9 +++++++++ tests/child-process.ts | 4 ++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/child-process.ts diff --git a/src/bin.ts b/src/bin.ts index 166ed0f80..0de77e1cb 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -139,13 +139,15 @@ const EVAL_FILENAME = `[eval].ts` const EVAL_PATH = join(cwd, EVAL_FILENAME) const EVAL_INSTANCE = { input: '', output: '', version: 0, lines: 0 } +// Prepend `ts-node` arguments to CLI for child processes. +process.argv = [process.argv[1]].concat(args._.length ? resolve(cwd, args._[0]) : []).concat(args._.slice(1)) +process.execArgv.unshift(__filename, ...process.argv.slice(2, process.argv.length - args._.length)) + // Execute the main contents (either eval, script or piped). if (code) { evalAndExit(code, isPrinted) } else { if (args._.length) { - process.argv = ['node'].concat(resolve(cwd, args._[0])).concat(args._.slice(1)) - process.execArgv.unshift(__filename) Module.runMain() } else { // Piping of execution _only_ occurs when no other script is specified. diff --git a/src/index.spec.ts b/src/index.spec.ts index 565285fa4..4d0d5cfe4 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -260,6 +260,15 @@ describe('ts-node', function () { return done() }) }) + + it('should preserve `ts-node` context with child process', function (done) { + exec(`${BIN_EXEC} tests/child-process`, function (err, stdout) { + expect(err).to.equal(null) + expect(stdout).to.equal('Hello, world!\n') + + return done() + }) + }) }) describe('register', function () { diff --git a/tests/child-process.ts b/tests/child-process.ts new file mode 100644 index 000000000..05050fec5 --- /dev/null +++ b/tests/child-process.ts @@ -0,0 +1,4 @@ +import { join } from 'path' +import { fork } from 'child_process' + +fork(join(__dirname, 'hello-world.ts'))