diff --git a/bin/ts-node-dev b/bin/ts-node-dev index 7deae4a..e12562a 100644 --- a/bin/ts-node-dev +++ b/bin/ts-node-dev @@ -54,7 +54,8 @@ var opts = minimist(devArgs, { 'interval', 'debounce', 'watch', - 'restart-terminated' + 'restart-terminated', + 'graceful_ipc' ], alias: { transpileOnly: 'T', diff --git a/lib/index.js b/lib/index.js index d6f61f6..6907e54 100755 --- a/lib/index.js +++ b/lib/index.js @@ -174,6 +174,11 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) { }) compiler.writeReadyFile() } + const sendGracefulIPC = (graceful_ipc) => { + if (!child) return + log.debug(`Sending "${graceful_ipc}" ipc message to child`) + child.send(graceful_ipc); + } const killChild = () => { if (!child) return log.debug('Sending SIGTERM kill to child pid', child.pid) @@ -191,11 +196,15 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) { child.stopping = true child.respawn = true if (child.connected === undefined || child.connected === true) { - log.debug('Disconnecting from child') - child.disconnect() - //if (!willTerminate) { - killChild() - //} + if(opts.graceful_ipc){ + sendGracefulIPC(opts.graceful_ipc) + } else { + log.debug('Disconnecting from child') + child.disconnect() + //if (!willTerminate) { + killChild() + //} + } } } @@ -237,7 +246,11 @@ module.exports = function(script, scriptArgs, nodeArgs, opts) { // Relay SIGTERM process.on('SIGTERM', function() { log.debug('Process got SIGTERM') - killChild() + if(opts.graceful_ipc){ + sendGracefulIPC(opts.graceful_ipc) + } else { + killChild() + } if (opts['restart-terminated']) { var timeout = opts['restart-terminated'] || 0 log.info('Restarting terminated in ' + timeout + ' seconds') diff --git a/test/fixture/graceful-ipc.ts b/test/fixture/graceful-ipc.ts new file mode 100644 index 0000000..b78c0ce --- /dev/null +++ b/test/fixture/graceful-ipc.ts @@ -0,0 +1,7 @@ +console.log('v1') + +process.on('message', function(m) { + console.log(m); + process.disconnect(); + process.exit(); +}) \ No newline at end of file diff --git a/test/index.ts b/test/index.ts index 54c5dc8..ef1d82a 100644 --- a/test/index.ts +++ b/test/index.ts @@ -172,3 +172,19 @@ test('It allows use custom TS Transformers', async (t) => { await ps.waitForLine(/transformed/) await ps.exit() }) + +// TODO: fix --graceful_ipc option won't work with spawnTsNodeDev() +// p.s the option works when manually running node on the terminal like this: +// node .\bin\ts-node-dev --graceful_ipc=AWESOME .\test\fixture\graceful-ipc.ts +test.skip('It send IPC message during restart events', async (t) => { + const ps = spawnTsNodeDev('--graceful_ipc="GRACETERM" graceful-ipc.ts') + + // changing the file to trigger restart + setTimeout(() => replaceText('graceful-ipc.ts', 'v1', 'v2'), 250) + + await ps.waitForLine(/GRACETERM/) + + await ps.exit() + + replaceText('graceful-ipc.ts', 'v2', 'v1') +})