Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/restart non watch #1384

Merged
merged 4 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 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,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 @@ -213,7 +207,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 +221,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
Original file line number Diff line number Diff line change
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