diff --git a/lib/monitor/run.js b/lib/monitor/run.js index 2baebff4..a2a0f29f 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -4,6 +4,7 @@ var bus = utils.bus; var childProcess = require('child_process'); var spawn = childProcess.spawn; var exec = childProcess.exec; +var fork = childProcess.fork; var watch = require('./watch').watch; var config = require('../config'); var child = null; // the actual child process we spawn @@ -82,7 +83,16 @@ function run(options) { }); } - child = spawn.apply(null, spawnArgs); + if (executable === 'node') { + var forkArgs = cmd.args.slice(1); + var env = utils.merge(options.execOptions.env, process.env); + child = fork(options.execOptions.script, forkArgs, { + env: env, + silent: true, + }); + } else { + child = spawn.apply(null, spawnArgs); + } if (config.required) { var emit = { @@ -227,10 +237,13 @@ function run(options) { callback(); }); } - } else if (!noRestart) { + } else if (!noRestart && executable !== 'node') { // if there's no child, then we need to manually start the process // this is because as there was no child, the child.on('exit') event // handler doesn't exist which would normally trigger the restart. + // + // We only perform this for non-node processes since `fork` automatically + // handles cleanup of child processes when the parent dies. bus.once('start', callback); restart(); } else { diff --git a/lib/monitor/watch.js b/lib/monitor/watch.js index 1a8f0918..d9f870a5 100644 --- a/lib/monitor/watch.js +++ b/lib/monitor/watch.js @@ -146,6 +146,8 @@ function filterAndRestart(files) { // reset the last check so we're only looking at recently modified files config.lastStarted = Date.now(); + console.log(matched.result); + if (matched.result.length) { if (config.options.delay > 0) { utils.log.detail('delaying restart for ' + config.options.delay + 'ms'); diff --git a/test/monitor/run.test.js b/test/monitor/run.test.js index 27149548..572f63ab 100644 --- a/test/monitor/run.test.js +++ b/test/monitor/run.test.js @@ -153,32 +153,29 @@ describe('when nodemon runs (2)', function () { }, 1500); }); - // it('should kill child on SIGINT', function (done) { - // fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)'); - - // nodemon({ script: tmp, verbose: true }).on('start', function () { - // assert(true, 'nodemon is waiting for a change'); - - // setTimeout(function () { - // // process.once('SIGINT', function () { - // // // do nothing - // // console.log('not going to exit'); - // // }); - - // process.kill(process.pid, 'SIGINT'); - // }, 1000); - // }).on('crash', function () { - // assert(false, 'detected crashed state'); - // }).on('exit', function () { - // assert(true, 'quit correctly'); - // nodemon.reset(); - // done(); - - // setTimeout(function () { - // process.kill(process.pid, 'SIGINT'); - // }, 1000); - - // }); - - // }); + it('should kill child on SIGINT', function (done) { + fs.writeFileSync(tmp, 'setTimeout(function () { var n = 10; }, 10000)'); + + nodemon({ script: tmp, verbose: true }).on('start', function () { + assert(true, 'nodemon is waiting for a change'); + + setTimeout(function () { + process.once('SIGINT', function () { + // do nothing + }); + + process.kill(process.pid, 'SIGINT'); + }, 1000); + }).on('crash', function () { + assert(false, 'detected crashed state'); + }).on('exit', function () { + assert(true, 'quit correctly'); + nodemon.reset(); + done(); + + setTimeout(function () { + process.kill(process.pid, 'SIGINT'); + }, 1000); + }); + }); });