Skip to content

Commit

Permalink
Preserve execArgv with ts-node arguments (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jan 22, 2019
1 parent 157bb4c commit b39b38d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.execArgv.unshift(__filename, ...process.argv.slice(2, process.argv.length - args._.length))
process.argv = [process.argv[1]].concat(args._.length ? resolve(cwd, args._[0]) : []).concat(args._.slice(1))

// 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.
Expand Down
9 changes: 9 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
4 changes: 4 additions & 0 deletions tests/child-process.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { join } from 'path'
import { fork } from 'child_process'

fork(join(__dirname, 'hello-world.ts'))

0 comments on commit b39b38d

Please sign in to comment.