fix: don't watch directory when watching file

Fixes #1320

Also refactors the fix for #1259
This commit is contained in:
Remy Sharp
2018-07-10 18:40:48 +01:00
committed by GitHub
parent 9d1a63c6a6
commit d48a482b47
2 changed files with 35 additions and 22 deletions

View File

@@ -7,7 +7,6 @@ var chokidar = require('chokidar');
var undefsafe = require('undefsafe');
var config = require('../config');
var path = require('path');
const fs = require('fs');
var utils = require('../utils');
var bus = utils.bus;
var match = require('./match');
@@ -36,7 +35,6 @@ function watch() {
const rootIgnored = config.options.ignore;
debugRoot('ignored', rootIgnored);
var promises = [];
var watchedFiles = [];
const promise = new Promise(function (resolve) {
@@ -54,16 +52,6 @@ function watch() {
ignored.push(dotFilePattern);
}
dirs = dirs.map(dir => {
// if the directory is a file, it somehow causes
// windows to lose the filename upon change
if (fs.statSync(dir).isFile()) {
dir = path.dirname(dir);
}
return dir;
});
var watchOptions = {
ignorePermissionErrors: true,
ignored: ignored,
@@ -124,10 +112,12 @@ function watch() {
});
return promise.catch(e => {
// this is a core error and it should break nodemon - so I have to break
// out of a promise using the setTimeout
setTimeout(() => {
throw e;
});
}).then(function (res) {
}).then(function () {
utils.log.detail(`watching ${watchedFiles.length} file${
watchedFiles.length === 1 ? '' : 's'}`);
return watchedFiles;
@@ -138,6 +128,7 @@ function filterAndRestart(files) {
if (!Array.isArray(files)) {
files = [files];
}
if (files.length) {
var cwd = process.cwd();
if (this.options && this.options.cwd) {
@@ -147,20 +138,23 @@ function filterAndRestart(files) {
utils.log.detail(
'files triggering change check: ' +
files
.map(function (file) {
.map(file => {
const res = path.relative(cwd, file);
return res;
})
.join(', ')
);
files = files.map(file => {
// make sure the path is right and drop an empty
// filenames (sometimes on windows)
files = files.filter(Boolean).map(file => {
return path.relative(process.cwd(), path.relative(cwd, file));
});
if (utils.isWindows) {
// ensure the drive letter is in uppercase (c:\foo -> C:\foo)
files = files.map(function (f) {
files = files.map(f => {
if (f.indexOf(':') === -1) { return f; }
return f[0].toUpperCase() + f.slice(1);
});
}
@@ -218,7 +212,7 @@ function filterAndRestart(files) {
function restartBus(matched) {
utils.log.status('restarting due to changes...');
matched.result.map(function (file) {
matched.result.map(file => {
utils.log.detail(path.relative(process.cwd(), file));
});
@@ -232,11 +226,9 @@ function restartBus(matched) {
function debounce(fn, delay) {
var timer = null;
return function () {
var context = this;
var args = arguments;
const context = this;
const args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
timer = setTimeout(() =>fn.apply(context, args), delay);
};
}

View File

@@ -38,6 +38,27 @@ describe('watch count', function () {
});
});
it('should not watch directory when given a single file', function (done) {
process.chdir('test/fixtures/watch-count/');
var watching = 0;
nodemon({ script: appjs, verbose: true, watch: appjs }).on('start', function () {
setTimeout(function () {
assert(watching === 1, `got ${watching} files`);
nodemon.once('exit', done).emit('quit');
}, 200);
}).on('watching', file => {
watching++;
}).on('log', function (data) {
var match = null;
var count = 0;
if (match = data.message.match(watchRe)) {
count = match[1].replace(',', '') * 1;
assert(count === 1, `log showing ${count} files`);
}
});
});
it('should ignore node_modules from any dir', function (done) {
process.chdir('test/fixtures/watch-count/lib');
nodemon({ script: appjs, verbose: true, watch: '..' }).on('start', function () {