Skip to content

Commit

Permalink
signals: ignore them
Browse files Browse the repository at this point in the history
signals are passed to the spawned child process; this means
that the main process should not react to them, and instead
wait for the spawned process to exit as a result of receiving
the forwarded signal.
  • Loading branch information
stelcheck committed Sep 8, 2017
1 parent 2ee9575 commit d3e5eb2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"clean": "rimraf dist",
"tsc": "tsc",
"build": "npm run clean && npm run tsc",
"test-spec": "mocha dist/**/*.spec.js -R spec --bail",
"test-spec": "mocha --reporter mocha-reporter dist/**/*.spec.js --bail",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- \"dist/**/*.spec.js\" -R spec --bail",
"test": "npm run build && npm run lint && npm run test-cov",
"prepublish": "typings install && npm run build"
Expand Down Expand Up @@ -70,6 +70,7 @@
"make-error": "^1.1.1",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"mocha-reporter": "0.0.3",
"source-map-support": "^0.4.0",
"tsconfig": "^7.0.0",
"v8flags": "^3.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ v8flags(function (err, v8flags) {
{ stdio: 'inherit' }
)

// Ignore the following signals; the child process will be receiving them,
// and should exit by itself
process.on('SIGINT', () => true)
process.on('SIGTERM', () => true)

proc.on('exit', function (code: number, signal: string) {
process.on('exit', function () {
if (signal) {
Expand Down
20 changes: 19 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { exec } from 'child_process'
import { exec, spawn } from 'child_process'
import { join } from 'path'
import semver = require('semver')
import ts = require('typescript')
Expand All @@ -20,6 +20,24 @@ describe('ts-node', function () {
})

describe('cli', function () {
it('should not react to signals, but forward them to the child process', function (done) {
const proc = spawn('node', [
EXEC_PATH,
'./tests/signals.ts'
])

proc.stdout.on('data', (data) => {
const message = data.toString()
expect(message).to.be.eq('exit')
done()
})

proc.stderr.on('data', (data) => console.log(data.toString()))

proc.on('exit', (code) => expect(code).to.be.eq(null))
setTimeout(() => proc.kill('SIGTERM'), 100)
})

it('should execute cli', function (done) {
exec(`${BIN_EXEC} tests/hello-world`, function (err, stdout) {
expect(err).to.equal(null)
Expand Down
8 changes: 8 additions & 0 deletions tests/signals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
process.on('SIGINT', () => {
setTimeout(() => {
console.log('exited')
process.exit(0)
}, 500)
})

setInterval(() => console.log('should not be reached'), 2000)

0 comments on commit d3e5eb2

Please sign in to comment.