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 ee2aac1 commit d1c32ad
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions lib/monitor/watch.js
Original file line number Diff line number Diff line change
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 @@ -49,16 +47,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 @@ -119,10 +107,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 @@ -133,6 +123,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 @@ -142,20 +133,22 @@ 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 @@ -213,7 +206,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 @@ -227,11 +220,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);
};
}

0 comments on commit d1c32ad

Please sign in to comment.