From 88954455becf55660f2d28e700d305ee7035f738 Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Thu, 15 Feb 2018 21:57:47 +0000 Subject: [PATCH] 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 --- .gitignore | 3 +- lib/monitor/run.js | 4 +- lib/monitor/watch.js | 9 +- test/fixtures/1246/app/index.js | 2 + test/fixtures/1246/watching/index.js | 2 + test/monitor/watch-restart.test.js | 194 ++++++++++++++++++--------- 6 files changed, 143 insertions(+), 71 deletions(-) create mode 100644 test/fixtures/1246/app/index.js create mode 100644 test/fixtures/1246/watching/index.js diff --git a/.gitignore b/.gitignore index caabbcf8..64f3f294 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,7 @@ npm-debug.log node_modules coverage tmp +issues/ test/fixtures/test* -.idea \ No newline at end of file +.idea diff --git a/lib/monitor/run.js b/lib/monitor/run.js index 76fff484..930b3099 100644 --- a/lib/monitor/run.js +++ b/lib/monitor/run.js @@ -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) { diff --git a/lib/monitor/watch.js b/lib/monitor/watch.js index b3838fd9..6fd5b9aa 100644 --- a/lib/monitor/watch.js +++ b/lib/monitor/watch.js @@ -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( diff --git a/test/fixtures/1246/app/index.js b/test/fixtures/1246/app/index.js new file mode 100644 index 00000000..2d3a9903 --- /dev/null +++ b/test/fixtures/1246/app/index.js @@ -0,0 +1,2 @@ +require('http').createServer((req, res) => res.end('ok')).listen(8000); + diff --git a/test/fixtures/1246/watching/index.js b/test/fixtures/1246/watching/index.js new file mode 100644 index 00000000..2d3a9903 --- /dev/null +++ b/test/fixtures/1246/watching/index.js @@ -0,0 +1,2 @@ +require('http').createServer((req, res) => res.end('ok')).listen(8000); + diff --git a/test/monitor/watch-restart.test.js b/test/monitor/watch-restart.test.js index 33cfc7f6..529600c7 100644 --- a/test/monitor/watch-restart.test.js +++ b/test/monitor/watch-restart.test.js @@ -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); }); -}); \ No newline at end of file + 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); + }); + }); +});