diff --git a/src/common.js b/src/common.js index c8d7264..b23795f 100644 --- a/src/common.js +++ b/src/common.js @@ -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]; } diff --git a/src/node_watcher.js b/src/node_watcher.js index 817b49b..f50cd89 100644 --- a/src/node_watcher.js +++ b/src/node_watcher.js @@ -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 @@ -27,7 +28,7 @@ module.exports = NodeWatcher; * Watches `dir`. * * @class NodeWatcher - * @param String dir + * @param {String} dir * @param {Object} opts * @public */ @@ -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 ); } @@ -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() { diff --git a/test/test.js b/test/test.js index fa8031a..26fb952 100755 --- a/test/test.js +++ b/test/test.js @@ -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, {