fix: make watch & ignore relative (#1253)

* 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
This commit is contained in:
Remy Sharp
2018-02-15 21:57:47 +00:00
committed by GitHub
parent 5e88b04eaa
commit 88954455be
6 changed files with 143 additions and 71 deletions

3
.gitignore vendored
View File

@@ -17,6 +17,7 @@ npm-debug.log
node_modules
coverage
tmp
issues/
test/fixtures/test*
.idea
.idea

View File

@@ -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) {

View File

@@ -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,
@@ -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 () {
@@ -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
@@ -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(

2
test/fixtures/1246/app/index.js vendored Normal file
View File

@@ -0,0 +1,2 @@
require('http').createServer((req, res) => res.end('ok')).listen(8000);

2
test/fixtures/1246/watching/index.js vendored Normal file
View File

@@ -0,0 +1,2 @@
require('http').createServer((req, res) => res.end('ok')).listen(8000);

View File

@@ -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;');
@@ -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;
@@ -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);
});
@@ -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);
});
});
});