@@ -16,6 +16,7 @@ import { makeLog } from './log'
16
16
const version = require ( '../package.json' ) . version
17
17
const tsNodeVersion = require ( 'ts-node' ) . VERSION
18
18
const tsVersion = require ( 'typescript' ) . version
19
+ const signals : NodeJS . Signals [ ] = [ 'SIGINT' , 'SIGTERM' ]
19
20
20
21
export const runDev = (
21
22
script : string ,
@@ -118,6 +119,7 @@ export const runDev = (
118
119
child = fork ( cmd [ 0 ] , cmd . slice ( 1 ) , {
119
120
cwd : process . cwd ( ) ,
120
121
env : process . env ,
122
+ detached : true ,
121
123
} )
122
124
123
125
starting = false
@@ -167,11 +169,15 @@ export const runDev = (
167
169
compiler . compile ( message )
168
170
} )
169
171
170
- child . on ( 'exit' , function ( code ) {
171
- log . debug ( 'Child exited with code %s' , code )
172
- if ( ! child ) return
173
- if ( ! child . respawn ) process . exit ( code || 0 )
174
- child = undefined
172
+ child . on ( 'close' , ( code : number , signal : string ) => {
173
+ log . debug ( 'Child closed with code %s' , code )
174
+ if ( signal ) {
175
+ log . debug ( `Exiting process with signal ${ signal } ` )
176
+ process . kill ( process . pid , signal )
177
+ } else {
178
+ log . debug ( `Exiting process with code ${ code } ` )
179
+ process . exit ( code )
180
+ }
175
181
} )
176
182
177
183
if ( cfg . respawn ) {
@@ -203,14 +209,14 @@ export const runDev = (
203
209
} )
204
210
compiler . writeReadyFile ( )
205
211
}
206
- const killChild = ( ) => {
212
+ const killChild = ( signal : NodeJS . Signals ) => {
207
213
if ( ! child ) return
208
- log . debug ( ' Sending SIGTERM kill to child pid' , child . pid )
214
+ log . debug ( ` Sending ${ signal } to child pid` , child . pid )
209
215
if ( opts [ 'tree-kill' ] ) {
210
216
log . debug ( 'Using tree-kill' )
211
217
kill ( child . pid )
212
218
} else {
213
- child . kill ( 'SIGTERM' )
219
+ child . kill ( signal )
214
220
}
215
221
}
216
222
function stop ( willTerminate ?: boolean ) {
@@ -223,8 +229,8 @@ export const runDev = (
223
229
log . debug ( 'Disconnecting from child' )
224
230
child . disconnect ( )
225
231
if ( ! willTerminate ) {
226
- killChild ( )
227
232
}
233
+ killChild ( 'SIGTERM' )
228
234
}
229
235
}
230
236
@@ -261,11 +267,17 @@ export const runDev = (
261
267
}
262
268
}
263
269
264
- // Relay SIGTERM
265
- process . on ( 'SIGTERM' , function ( ) {
266
- log . debug ( 'Process got SIGTERM' )
267
- killChild ( )
268
- process . exit ( 0 )
270
+ signals . forEach ( ( signal : NodeJS . Signals ) =>
271
+ process . on ( signal , ( ) => {
272
+ log . debug ( `Process got ${ signal } , killing child` )
273
+ killChild ( signal )
274
+ } )
275
+ )
276
+
277
+ process . on ( 'exit' , ( ) => {
278
+ if ( child ) {
279
+ child . kill ( )
280
+ }
269
281
} )
270
282
271
283
const compiler = makeCompiler ( opts , {
0 commit comments