diff --git a/bin/nodemon.js b/bin/nodemon.js index 01b3060..3867539 100755 --- a/bin/nodemon.js +++ b/bin/nodemon.js @@ -12,6 +12,5 @@ if (notifier.update) { } var options = cli.parse(process.argv); -options.restartable = 'rs'; nodemon(options); \ No newline at end of file diff --git a/lib/config/defaults.js b/lib/config/defaults.js index 4bb3e3d..df24f9a 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -1,5 +1,6 @@ // default options for config.options module.exports = { + restartable: 'rs', execMap: { py: 'python', rb: 'ruby' diff --git a/lib/config/index.js b/lib/config/index.js index 8c02fc2..e2472d9 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -25,6 +25,7 @@ function reset() { } var config = { + run: false, system: { noWatch: false, watchWorks: false, diff --git a/lib/monitor/run.js b/lib/monitor/run.js index b913bad..3b912ca 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -152,6 +152,9 @@ run.kill = noop; run.restart = noop; bus.on('quit', function () { + // immediately try to stop any polling + config.run = false; + // remove event listener var exit = function () { exit = noop; // null out in case of race condition diff --git a/lib/monitor/watch.js b/lib/monitor/watch.js index 61e12a6..ce70cf1 100644 --- a/lib/monitor/watch.js +++ b/lib/monitor/watch.js @@ -157,11 +157,13 @@ function filterAndRestart(files) { } if (config.system.noWatch || config.options.forceLegacyWatch) { - setTimeout(monitor, config.timeout); + if (config.run) { + setTimeout(watch, config.timeout); + } } } -var monitor = module.exports = function () { +var watch = module.exports = function () { // if we have noWatch or watchWorks (i.e. not using `find`) // then called `changeFunction` which is local to this script if ((config.system.noWatch || config.system.watchWorks) && !config.options.forceLegacyWatch) { diff --git a/lib/nodemon.js b/lib/nodemon.js index 9b603d7..9059700 100644 --- a/lib/nodemon.js +++ b/lib/nodemon.js @@ -125,6 +125,7 @@ function nodemon(settings) { } } + config.run = true; monitor.run(config.options); }); @@ -148,7 +149,7 @@ nodemon.once = function (event, handler) { if (!eventHandlers[event]) { eventHandlers[event] = []; } eventHandlers[event].push(handler); bus.once(event, function () { - eventHandlers[event].splice(eventHandlers[event].indexOf(handler), 0); + eventHandlers[event].splice(eventHandlers[event].indexOf(handler), 1); handler.apply(this, arguments); }); return nodemon; @@ -166,7 +167,7 @@ nodemon.removeAllListners = function (event) { }).forEach(function (event) { eventHandlers[event].forEach(function (handler) { bus.removeListener(event, handler); - eventHandlers[event].splice(eventHandlers[event].indexOf(handler), 0); + eventHandlers[event].splice(eventHandlers[event].indexOf(handler), 1); }); // delete eventHandlers[event]; }); @@ -177,6 +178,7 @@ nodemon.removeAllListners = function (event) { nodemon.reset = function () { nodemon.removeAllListners(); utils.reset(); + config.run = false; }; // expose the full config diff --git a/lib/utils/log.js b/lib/utils/log.js index 31fc91c..e2eb1e5 100644 --- a/lib/utils/log.js +++ b/lib/utils/log.js @@ -51,7 +51,7 @@ Logger.prototype.required = function (val) { Logger.prototype.debug = false; Logger.prototype._log = function (type, msg) { if (required) { - bus.emit('log', { type: type, message: msg, colour: msg }); + bus.emit('log', { type: type, message: msg || '', colour: msg || '' }); } else if (type === 'error') { util.error(msg); } else { diff --git a/test/config/load.test.js b/test/config/load.test.js index 1424565..1820601 100644 --- a/test/config/load.test.js +++ b/test/config/load.test.js @@ -18,10 +18,12 @@ describe('config load', function () { utils.home = oldhome; }); - after(function () { + after(function (done) { // clean up just in case. - nodemon.emit('quit'); - nodemon.reset(); + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); }); diff --git a/test/fork/watch-restart.test.js b/test/fork/watch-restart.test.js index b879cc8..9701d52 100644 --- a/test/fork/watch-restart.test.js +++ b/test/fork/watch-restart.test.js @@ -54,7 +54,6 @@ describe('nodemon fork child restart', function () { }, output: function (data) { var msg = colour.strip(data.trim()); - console.log(data.trim()); if (utils.match(msg, 'changes after filters')) { var changes = msg.slice(-5).split('/'); var restartedOn = changes.pop(); diff --git a/test/lib/require.test.js b/test/lib/require.test.js index b9c86e6..ea34d27 100644 --- a/test/lib/require.test.js +++ b/test/lib/require.test.js @@ -7,9 +7,11 @@ var nodemon = require('../../lib/'), appjs = path.resolve(__dirname, '..', 'fixtures', 'app.js'); describe('require-able', function () { - afterEach(function (){ - nodemon.emit('quit'); - nodemon.reset() + afterEach(function (done) { + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); }); it('should know nodemon has been required', function () { @@ -28,7 +30,7 @@ describe('require-able', function () { nodemon.emit('quit'); }).on('quit', function () { assert(restarted, 'nodemon restarted and quit properly'); - nodemon.reset() + nodemon.reset(); done(); }).on('log', function (event) { // console.log(event.message); @@ -47,8 +49,8 @@ describe('require-able', function () { nodemon.emit('quit'); }).on('quit', function () { assert(restarted); + nodemon.reset(); // unbind events for testing again - nodemon.reset() done(); }); }); diff --git a/test/monitor/run.test.js b/test/monitor/run.test.js index 7ed0761..9ffb937 100644 --- a/test/monitor/run.test.js +++ b/test/monitor/run.test.js @@ -10,11 +10,13 @@ var nodemon = require('../../lib/'), describe('when nodemon runs', function () { var tmp = path.resolve('test/fixtures/test' + crypto.randomBytes(16).toString('hex') + '.js'); - after(function () { + after(function (done) { fs.unlink(tmp); // clean up just in case. - nodemon.emit('quit'); - nodemon.reset() + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); }); it('should wait when the script crashes', function (done) { @@ -28,9 +30,10 @@ describe('when nodemon runs', function () { }, 1000); }).on('restart', function () { assert(true, 'nodemon restarted'); - nodemon.emit('quit'); - nodemon.reset() - done(); + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); }); }); @@ -47,9 +50,10 @@ describe('when nodemon runs', function () { }, 500); }).on('restart', function () { assert(true, 'nodemon restarted'); - nodemon.emit('quit'); - nodemon.reset() - done(); + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); }); }); @@ -71,8 +75,7 @@ describe('when nodemon runs', function () { assert(false, 'detected crashed state'); }).on('exit', function () { assert(true, 'quit correctly'); - nodemon.emit('quit'); - nodemon.reset() + nodemon.reset(); done(); setTimeout(function () { diff --git a/test/monitor/watch-restart.test.js b/test/monitor/watch-restart.test.js index 60cf206..a17af20 100644 --- a/test/monitor/watch-restart.test.js +++ b/test/monitor/watch-restart.test.js @@ -25,9 +25,10 @@ describe('nodemon monitor child restart', function () { fs.unlink(tmpjs); fs.unlink(tmpmd); // clean up just in case. - bus.once('exit', done); - nodemon.emit('quit'); - nodemon.reset(); + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); }); it('should happen when monitoring a single extension', function (done) { @@ -39,15 +40,16 @@ describe('nodemon monitor child restart', function () { }, 1000); }).on('restart', function () { assert(true, 'nodemon restarted'); - bus.once('exit', done); - nodemon.emit('quit'); - nodemon.reset(); + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); }); }); it('should happen when monitoring multiple extensions', function (done) { + write(true); setTimeout(function () { - write(true); nodemon({ script: tmpjs, @@ -64,9 +66,10 @@ describe('nodemon monitor child restart', function () { var changes = msg.trim().slice(-5).split('/'); var restartedOn = changes.pop(); assert(restartedOn === '1', 'nodemon restarted on a single file change'); - bus.once('exit', done); - nodemon.emit('quit'); - nodemon.reset(); + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); } }); }, 2000); @@ -91,9 +94,10 @@ describe('nodemon monitor child restart', function () { var changes = msg.trim().slice(-5).split('/'); var restartedOn = changes.pop(); assert(restartedOn === '1', 'nodemon restarted when watched directory'); - nodemon.once('exit', done); - nodemon.emit('quit'); - nodemon.reset(); + nodemon.once('exit', function () { + nodemon.reset(); + done(); + }).emit('quit'); } }); }, 2000);