Skip to content

Commit b39b38d

Browse files
authored
Preserve execArgv with ts-node arguments (#760)
1 parent 157bb4c commit b39b38d

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/bin.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,15 @@ const EVAL_FILENAME = `[eval].ts`
139139
const EVAL_PATH = join(cwd, EVAL_FILENAME)
140140
const EVAL_INSTANCE = { input: '', output: '', version: 0, lines: 0 }
141141

142+
// Prepend `ts-node` arguments to CLI for child processes.
143+
process.execArgv.unshift(__filename, ...process.argv.slice(2, process.argv.length - args._.length))
144+
process.argv = [process.argv[1]].concat(args._.length ? resolve(cwd, args._[0]) : []).concat(args._.slice(1))
145+
142146
// Execute the main contents (either eval, script or piped).
143147
if (code) {
144148
evalAndExit(code, isPrinted)
145149
} else {
146150
if (args._.length) {
147-
process.argv = ['node'].concat(resolve(cwd, args._[0])).concat(args._.slice(1))
148-
process.execArgv.unshift(__filename)
149151
Module.runMain()
150152
} else {
151153
// Piping of execution _only_ occurs when no other script is specified.

src/index.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,15 @@ describe('ts-node', function () {
260260
return done()
261261
})
262262
})
263+
264+
it('should preserve `ts-node` context with child process', function (done) {
265+
exec(`${BIN_EXEC} tests/child-process`, function (err, stdout) {
266+
expect(err).to.equal(null)
267+
expect(stdout).to.equal('Hello, world!\n')
268+
269+
return done()
270+
})
271+
})
263272
})
264273

265274
describe('register', function () {

tests/child-process.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { join } from 'path'
2+
import { fork } from 'child_process'
3+
4+
fork(join(__dirname, 'hello-world.ts'))

0 commit comments

Comments
 (0)