Skip to content

Commit

Permalink
fix: don't watch directory when watching file
Browse files Browse the repository at this point in the history
Fixes #1320

Also refactors the fix for #1259
  • Loading branch information
remy committed Jul 10, 2018
1 parent 9d1a63c commit d48a482
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
36 changes: 14 additions & 22 deletions lib/monitor/watch.js
Expand Up @@ -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');
Expand Down Expand Up @@ -36,7 +35,6 @@ function watch() {
const rootIgnored = config.options.ignore;
debugRoot('ignored', rootIgnored);

var promises = [];
var watchedFiles = [];

const promise = new Promise(function (resolve) {
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
});
}
Expand Down Expand Up @@ -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));
});

Expand All @@ -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);
};
}
21 changes: 21 additions & 0 deletions test/monitor/count.test.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit d48a482

Please sign in to comment.