Skip to content

Commit

Permalink
Use fileset (wrapper for glob) to support multi-patterns, including e…
Browse files Browse the repository at this point in the history
…xclusions
  • Loading branch information
shama committed Feb 14, 2013
1 parent eb20b6a commit 5578985
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 19 deletions.
54 changes: 38 additions & 16 deletions lib/gaze.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
var events = require('events');
var fs = require('fs');
var path = require('path');
var Glob = require('glob').Glob;
var fileset = require('fileset');
var minimatch = require('minimatch');
var Gaze;

Expand Down Expand Up @@ -275,17 +275,22 @@ Gaze.prototype.add = function(files, done) {
files = [files];
}
this._patterns = union(this._patterns, files);
forEachSeries(files, function(pattern, next) {
if (isEmpty(pattern)) { return; }
_this._glob = new Glob(pattern, _this.options, function(err, files) {
if (err) {
_this.emit('error', err);
return done(err);
}
_this._addToWatched(files);
next();
});
}, function() {

var include = [], exclude = [];
this._patterns.forEach(function(p) {
if (p.slice(0, 1) === '!') {
exclude.push(p.slice(1));
} else {
include.push(p);
}
});

fileset(include, exclude, _this.options, function(err, files) {
if (err) {
_this.emit('error', err);
return done(err);
}
_this._addToWatched(files);
_this.close(false);
_this._initWatched(done);
});
Expand Down Expand Up @@ -384,12 +389,27 @@ Gaze.prototype._addToWatched = function(files) {

// Returns true if the file matches this._patterns
Gaze.prototype._isMatch = function(file) {
var matched = false;
this._patterns.forEach(function(pattern) {
if (matched || (matched = minimatch(file, pattern)) ) {
return false;
var include = [], exclude = [];
this._patterns.forEach(function(p) {
if (p.slice(0, 1) === '!') {
exclude.push(p.slice(1));
} else {
include.push(p);
}
});
var matched = false, i = 0;
for (i = 0; i < include.length; i++) {
if (minimatch(file, include[i])) {
matched = true;
break;
}
}
for (i = 0; i < exclude.length; i++) {
if (minimatch(file, exclude[i])) {
matched = false;
break;
}
}
return matched;
};

Expand Down Expand Up @@ -472,6 +492,8 @@ Gaze.prototype._initWatched = function(done) {
}).forEach(function(file) {
// Is it a matching pattern?
var relFile = path.join(relDir, file);
// TODO: this can be optimized more
// we shouldnt need isMatch() and could just use add()
if (_this._isMatch(relFile)) {
// Add to watch then emit event
_this.add(relFile, function() {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"test": "grunt nodeunit -v"
},
"dependencies": {
"glob": "~3.1.14",
"minimatch": "~0.2.9"
"minimatch": "~0.2.9",
"fileset": "~0.1.5"
},
"devDependencies": {
"grunt": "~0.4.0rc6",
"grunt": "~0.4.0rc7",
"grunt-contrib-nodeunit": "~0.1.2rc6",
"grunt-contrib-jshint": "~0.1.1rc6",
"grunt-benchmark": "~0.1.1"
Expand Down
42 changes: 42 additions & 0 deletions test/patterns_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

var gaze = require('../lib/gaze.js');
var path = require('path');
var fs = require('fs');

// Clean up helper to call in setUp and tearDown
function cleanUp(done) {
[
'added.js',
'nested/added.js',
].forEach(function(d) {
var p = path.resolve(__dirname, 'fixtures', d);
if (fs.existsSync(p)) { fs.unlinkSync(p); }
});
done();
}

exports.patterns = {
setUp: function(done) {
process.chdir(path.resolve(__dirname, 'fixtures'));
cleanUp(done);
},
tearDown: cleanUp,
negate: function(test) {
test.expect(1);
gaze(['**/*.js', '!nested/**/*.js'], function(err, watcher) {
watcher.on('added', function(filepath) {
var expected = path.relative(process.cwd(), filepath);
test.equal(path.join('added.js'), expected);
watcher.close();
});
// dont add
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'nested', 'added.js'), 'var added = true;');
setTimeout(function() {
// should add
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'added.js'), 'var added = true;');
}, 1000);
watcher.on('end', test.done);
});
}
};

0 comments on commit 5578985

Please sign in to comment.