Skip to content

Commit

Permalink
fix: make watch & ignore relative (#1253)
Browse files Browse the repository at this point in the history
* docs: add sponsors

[skip ci]

* fix: make watch & ignore relative

Fixes #1246

* fix: relative parent watching and ignore

* chore: lint

* test: fix bail on options
  • Loading branch information
remy authored Feb 15, 2018
1 parent 5e88b04 commit 8895445
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 71 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ npm-debug.log
node_modules
coverage
tmp
issues/
test/fixtures/test*

.idea
.idea
4 changes: 2 additions & 2 deletions lib/monitor/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ function run(options) {
silent: !hasStdio,
});
utils.log.detail('forking');
debug(forkArgs);
debug('fork', sh, shFlag, args)
} else {
utils.log.detail('spawning');
child = spawn.apply(null, spawnArgs);
debug(spawnArgs);
debug('spawn', sh, shFlag, args)
}

if (config.required) {
Expand Down
9 changes: 7 additions & 2 deletions lib/monitor/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function watch() {

var watchOptions = {
ignorePermissionErrors: true,
cwd: process.cwd(), // use cwd for relative path ignore
cwd: dir,
ignored: ignored,
persistent: true,
usePolling: config.options.legacyWatch || false,
Expand Down Expand Up @@ -83,6 +83,7 @@ function watch() {
watchedFiles.push(file);
watchedFiles = Array.from(new Set(watchedFiles)); // ensure no dupes
total = watchedFiles.length;
bus.emit('watching', file);
debug('watching dir: %s', file);
});
watcher.on('ready', function () {
Expand Down Expand Up @@ -128,7 +129,7 @@ function filterAndRestart(files) {
});
}

var cwd = process.cwd();
var cwd = this.options ? this.options.cwd : process.cwd();
utils.log.detail(
'files triggering change check: ' +
files
Expand All @@ -138,6 +139,10 @@ function filterAndRestart(files) {
.join(', ')
);

files = files.map(file => {
return path.relative(process.cwd(), path.join(cwd, file));
});

debug('filterAndRestart on', files);

var matched = match(
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/1246/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('http').createServer((req, res) => res.end('ok')).listen(8000);

2 changes: 2 additions & 0 deletions test/fixtures/1246/watching/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('http').createServer((req, res) => res.end('ok')).listen(8000);

194 changes: 128 additions & 66 deletions test/monitor/watch-restart.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
'use strict';
/*global describe, it, after, afterEach */
var nodemon = require('../../lib/'),
assert = require('assert'),
fs = require('fs'),
utils = require('../utils'),
path = require('path'),
touch = require('touch'),
crypto = require('crypto'),
baseFilename = 'test/fixtures/test' + crypto.randomBytes(16).toString('hex');
let debugLogger = {};
const nodemon = require('../../lib/');
var assert = require('assert');
var fs = require('fs');
var utils = require('../utils');
var path = require('path');
var touch = require('touch');
var crypto = require('crypto');
var baseFilename =
'test/fixtures/test' + crypto.randomBytes(16).toString('hex');

var WAIT_BEFORE_START = 3000;

describe('nodemon monitor child restart', function () {
var tmpjs = path.resolve(baseFilename + '.js'),
tmpmd = path.resolve(baseFilename + '.md');
var tmpjs = path.resolve(baseFilename + '.js');
var tmpmd = path.resolve(baseFilename + '.md');

function write(both) {
fs.writeFileSync(tmpjs, 'true;');
Expand All @@ -22,10 +24,11 @@ describe('nodemon monitor child restart', function () {
}
}

var pwd = process.cwd(),
oldhome = utils.home;
var pwd = process.cwd();
var oldhome = utils.home;

afterEach(function () {
debugLogger = {};
process.chdir(pwd);
utils.home = oldhome;

Expand All @@ -38,51 +41,69 @@ describe('nodemon monitor child restart', function () {
});

after(function (done) {
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
nodemon
.once('exit', function () {
nodemon.reset(done);
})
.emit('quit');
});

it('should happen when monitoring a single extension', function (done) {
write();

setTimeout(function () {
nodemon({ script: tmpjs, verbose: true, ext: 'js' }).on('start', function () {
setTimeout(function () {
touch.sync(tmpjs);
}, 1500);
}).on('restart', function (files) {
assert(files[0] === tmpjs, 'nodemon restarted because of change to our file' + files);
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
});
nodemon({ script: tmpjs, verbose: true, ext: 'js' })
.on('start', function () {
setTimeout(function () {
touch.sync(tmpjs);
}, 1500);
})
.on('restart', function (files) {
assert(
files[0] === tmpjs,
'nodemon restarted because of change to our file' + files
);
nodemon
.once('exit', function () {
nodemon.reset(done);
})
.emit('quit');
});
}, WAIT_BEFORE_START);
});

it('should happen when monitoring multiple extensions', function (done) {
write(true);
setTimeout(function () {

nodemon({
script: tmpjs,
ext: 'js md',
verbose: true
}).on('start', function () {
setTimeout(function () {
touch.sync(tmpmd);
}, 1500);
}).on('log', function (event) {
var msg = event.message;
if (utils.match(msg, 'changes after filters')) {
var changes = msg.trim().slice(-5).split('/');
var restartedOn = changes.pop();
assert(restartedOn === '1', 'nodemon restarted on a single file change');
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
}
});
verbose: true,
})
.on('start', function () {
setTimeout(function () {
touch.sync(tmpmd);
}, 1500);
})
.on('log', function (event) {
var msg = event.message;
if (utils.match(msg, 'changes after filters')) {
var changes = msg
.trim()
.slice(-5)
.split('/');
var restartedOn = changes.pop();
assert(
restartedOn === '1',
'nodemon restarted on a single file change'
);
nodemon
.once('exit', function () {
nodemon.reset(done);
})
.emit('quit');
}
});
}, WAIT_BEFORE_START);
});

Expand All @@ -97,42 +118,83 @@ describe('nodemon monitor child restart', function () {
script: tmpjs,
verbose: true,
ext: 'js',
watch: ['*.js', 'global']
}).on('start', function () {
setTimeout(function () {
touch.sync(tmpjs);
}, 1000);
}).on('restart', function (files) {
assert(files.length === 1, 'nodemon restarted when watching directory');
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
});
watch: ['*.js', 'global'],
})
.on('start', function () {
setTimeout(function () {
touch.sync(tmpjs);
}, 1000);
})
.on('restart', function (files) {
assert(
files.length === 1,
'nodemon restarted when watching directory'
);
nodemon
.once('exit', function () {
nodemon.reset(done);
})
.emit('quit');
});
}, WAIT_BEFORE_START);
});
}


it('should restart when watching directory', function (done) {
write(true);

// process.chdir(process.cwd() + '/test/fixtures');

setTimeout(function () {
nodemon({
script: tmpjs,
verbose: true,
ext: 'js md',
watch: ['test/fixtures/']
}).on('start', function () {
setTimeout(function () {
touch.sync(tmpmd);
}, 1000);
}).on('restart', function (files) {
assert(files.length === 1, 'nodemon restarted when watching directory');
nodemon.once('exit', function () {
nodemon.reset(done);
}).emit('quit');
});
watch: ['test/'],
})
.on('start', function () {
setTimeout(function () {
touch.sync(tmpmd);
}, 1000);
})
.on('restart', function (files) {
assert(
files.length === 1,
'nodemon restarted when watching directory'
);
nodemon
.once('exit', function () {
nodemon.reset(done);
})
.emit('quit');
});
}, WAIT_BEFORE_START);
});

});
it('should ignore relative node_modules', done => {
write(true);

process.chdir(process.cwd() + '/test/fixtures/1246/app');

nodemon({
script: 'index.js',
watch: ['../'],
})
.on('watching', file => {
assert(
file.indexOf('/node_modules/') === -1,
`node_modules found: ${file}`
);
})
.on('start', () => {
// gentle timeout to wait for the files to finish reading
setTimeout(() => {
nodemon
.once('exit', function () {
nodemon.reset(done);
})
.emit('quit');
}, 1000);
});
});
});

0 comments on commit 8895445

Please sign in to comment.