Skip to content

Commit

Permalink
Add filterDir to node_walker in node_watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ro-savage committed Feb 15, 2017
1 parent d702d0a commit 75fe6e5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ exports.assignOptions = function(watcher, opts) {
opts = opts || {};
watcher.globs = opts.glob || [];
watcher.dot = opts.dot || false;
watcher.ignored = opts.ignored || false;

if (!Array.isArray(watcher.globs)) {
watcher.globs = [watcher.globs];
}
Expand Down
15 changes: 11 additions & 4 deletions src/node_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var walker = require('walker');
var common = require('./common');
var platform = require('os').platform();
var EventEmitter = require('events').EventEmitter;
var anymatch = require('anymatch');

/**
* Constants
Expand All @@ -27,7 +28,7 @@ module.exports = NodeWatcher;
* Watches `dir`.
*
* @class NodeWatcher
* @param String dir
* @param {String} dir
* @param {Object} opts
* @public
*/
Expand All @@ -47,7 +48,8 @@ function NodeWatcher(dir, opts) {
this.root,
this.watchdir,
this.register,
this.emit.bind(this, 'ready')
this.emit.bind(this, 'ready'),
this.ignored
);
}

Expand Down Expand Up @@ -335,13 +337,18 @@ NodeWatcher.prototype.emitEvent = function(type, file, stat) {
* Traverse a directory recursively calling `callback` on every directory.
*
* @param {string} dir
* @param {function} callback
* @param {function} dirCallback
* @param {function} fileCallback
* @param {function} endCallback
* @param {*} ignored
* @private
*/

function recReaddir(dir, dirCallback, fileCallback, endCallback) {
function recReaddir(dir, dirCallback, fileCallback, endCallback, ignored) {
walker(dir)
.filterDir(function(currentDir) {
return !anymatch(ignored, currentDir);
})
.on('dir', normalizeProxy(dirCallback))
.on('file', normalizeProxy(fileCallback))
.on('end', function() {
Expand Down
31 changes: 31 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,37 @@ function harness(mode) {
});
});

describe('sane(dir, ignored) - node_watcher directory ignore', function() {
beforeEach(function () {
var Watcher = getWatcherClass({}); // node_watcher only
this.watcher = new Watcher(
testdir,
{ ignored: [/sub_0/, function (file) {
return file.indexOf('sub_1') !== -1;
}] });
});

afterEach(function(done) {
this.watcher.close(done);
});

it('ignores folders', function (done) {
var i = 0;
this.watcher.on('change', function(filepath, dir) {
assert.ok(!filepath.match(/sub_(0|1)/), 'Found changes in ignored subdir sub_0 and/or sub_1');
assert.equal(dir, testdir);
if (++i == 2) done();
});
this.watcher.on('ready', function() {
fs.writeFileSync(jo(testdir, 'sub_0', 'file_1'), 'wow');
fs.writeFileSync(jo(testdir, 'sub_1', 'file_1'), 'wow');
fs.writeFileSync(jo(testdir, 'sub_2', 'file_1'), 'wow');
fs.writeFileSync(jo(testdir, 'sub_3', 'file_1'), 'wow');
fs.writeFileSync(jo(testdir, 'sub_4', 'file_1'), 'wow');
});
});
});

describe('sane shortcut alias', function () {
beforeEach(function () {
this.watcher = sane(testdir, {
Expand Down

0 comments on commit 75fe6e5

Please sign in to comment.