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

Modified ignored filter for node_watcher to avoid common exception #91

Merged
merged 1 commit into from
Feb 21, 2017
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
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
34 changes: 34 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,40 @@ 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;
}] });
this.watcher.doIgnore = function () { //overwrite standard ignore for test
return false;
};
});

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