Skip to content

Commit e441226

Browse files
committed
fix: restart on new files added to watched dir
Fixes #664 Also includes tests and quietens the force crash test (though unchanged).
1 parent ef916f6 commit e441226

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

lib/monitor/watch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var utils = require('../utils');
1212
var bus = utils.bus;
1313
var match = require('./match');
1414
var watchers = [];
15+
var ready = false;
1516
var debouncedBus;
1617

1718
bus.on('reset', resetWatchers);
@@ -22,6 +23,7 @@ function resetWatchers() {
2223
watcher.close();
2324
});
2425
watchers = [];
26+
ready = false;
2527
}
2628

2729

@@ -59,12 +61,18 @@ function watch() {
5961

6062
watcher.on('change', filterAndRestart);
6163
watcher.on('add', function (file) {
64+
if (ready) {
65+
return filterAndRestart(file);
66+
}
67+
6268
watchedFiles.push(file);
6369
total++;
6470
debug('watching dir: %s', file);
6571
});
6672
watcher.on('ready', function () {
73+
ready = true;
6774
resolve(total);
75+
debugRoot('watch is complete');
6876
});
6977

7078
watcher.on('error', function (error) {

test/monitor/run.test.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
'use strict';
2-
/*global describe:true, it: true, after: true */
3-
var nodemon = require('../../lib/'),
4-
assert = require('assert'),
5-
fs = require('fs'),
6-
path = require('path'),
7-
touch = require('touch'),
8-
crypto = require('crypto');
2+
/*global describe:true, it: true, after: true, beforeEach */
3+
var nodemon = require('../../lib/');
4+
var assert = require('assert');
5+
var fs = require('fs');
6+
var path = require('path');
7+
var touch = require('touch');
8+
var crypto = require('crypto');
9+
10+
function rnd() {
11+
return crypto.randomBytes(16).toString('hex');
12+
}
913

1014
describe('when nodemon runs (2)', function () {
11-
var tmp = path.resolve('test/fixtures/test' + crypto.randomBytes(16).toString('hex') + '.js');
15+
var tmp = path.resolve('test/fixtures/test' + rnd() + '.js');
1216

1317
after(function (done) {
1418
fs.unlink(tmp);
@@ -23,10 +27,26 @@ describe('when nodemon runs (2)', function () {
2327
nodemon.reset(done);
2428
});
2529

30+
it('should restart when new files are added', function (done) {
31+
fs.writeFileSync(tmp, 'setTimeout(true, 10000)');
32+
var tmp2 = path.resolve('test/fixtures/test' + rnd() + '-added.js');
33+
34+
nodemon({
35+
script: tmp,
36+
}).on('start', function () {
37+
setTimeout(function () {
38+
fs.writeFileSync(tmp2, 'setTimeout(true, 10000)');
39+
}, 500);
40+
}).on('restart', function () {
41+
assert(true, 'restarted after new file was added');
42+
nodemon.once('exit', done).emit('quit');
43+
});
44+
});
45+
2646
it('should wait when the script crashes', function (done) {
2747
fs.writeFileSync(tmp, 'throw Error("forced crash")');
2848

29-
nodemon({ script: tmp }).on('crash', function () {
49+
nodemon({ script: tmp, stdout: false }).on('crash', function () {
3050
assert(true, 'detected crashed state');
3151

3252
setTimeout(function () {
@@ -65,8 +85,9 @@ describe('when nodemon runs (2)', function () {
6585
var stdoutTestData = 'outputting some data';
6686
var stderrTestData = 'outputting an error';
6787

68-
var script = 'setTimeout(function () { console.log("' + stdoutTestData + '"); }, 5);' +
69-
'setTimeout(function () { console.error("' + stderrTestData + '"); }, 10);';
88+
var script = 'setTimeout(function () { console.log("' + stdoutTestData +
89+
'"); }, 5); setTimeout(function () { console.error("' + stderrTestData +
90+
'"); }, 10);';
7091

7192
fs.writeFileSync(tmp, script);
7293

@@ -78,8 +99,7 @@ describe('when nodemon runs (2)', function () {
7899

79100
nodemon({
80101
script: tmp,
81-
stdout: false
82-
102+
stdout: false,
83103
}).on('crash', function () {
84104
assert(false, 'detected crashed state');
85105

@@ -96,8 +116,10 @@ describe('when nodemon runs (2)', function () {
96116
var stdoutWritableResult = fs.readFileSync(stdoutFileName);
97117
var stderrWritableResult = fs.readFileSync(stderrFileName);
98118

99-
assert(stdoutWritableResult === stdoutTestData, 'stdout has been piped correctly');
100-
assert(stderrWritableResult === stderrTestData, 'stderr has been piped correctly');
119+
assert(stdoutWritableResult === stdoutTestData,
120+
'stdout has been piped correctly');
121+
assert(stderrWritableResult === stderrTestData,
122+
'stderr has been piped correctly');
101123

102124
this.emit('quit');
103125

@@ -112,7 +134,8 @@ describe('when nodemon runs (2)', function () {
112134
});
113135
});
114136

115-
it('should not run command on startup if runOnChangeOnly is true', function (done) {
137+
it('should not run command on startup if runOnChangeOnly is true',
138+
function (done) {
116139
fs.writeFileSync(tmp, 'console.log("testing 1 2 3")');
117140

118141
nodemon({

0 commit comments

Comments
 (0)