Skip to content

Commit

Permalink
Fixes shama#214 - Gaze.prototype._addToWatched repeating readDir of s…
Browse files Browse the repository at this point in the history
…ame directory on each call
  • Loading branch information
Ivan Nikitin committed Jun 28, 2016
1 parent bec9df3 commit aac0c51
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
25 changes: 13 additions & 12 deletions lib/gaze.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,26 +257,27 @@ Gaze.prototype._addToWatched = function (files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
var filepath = path.resolve(this.options.cwd, file);
var isDir = helper.isDir(file);

var dirname = (helper.isDir(file)) ? filepath : path.dirname(filepath);
var dirname = isDir ? filepath : path.dirname(filepath);
dirname = helper.markDir(dirname);

// If a new dir is added
if (helper.isDir(file) && !(filepath in this._watched)) {
helper.objectPush(this._watched, filepath, []);
if (!(dirname in this._watched)) {
helper.objectPush(this._watched, dirname, []);

// add folders into the mix
var readdir = fs.readdirSync(dirname);
for (var j = 0; j < readdir.length; j++) {
var dirfile = path.join(dirname, readdir[j]);
if (fs.lstatSync(dirfile).isDirectory()) {
helper.objectPush(this._watched, dirname, dirfile + path.sep);
}
}
}

if (file.slice(-1) === '/') { filepath += path.sep; }
helper.objectPush(this._watched, path.dirname(filepath) + path.sep, filepath);

// add folders into the mix
var readdir = fs.readdirSync(dirname);
for (var j = 0; j < readdir.length; j++) {
var dirfile = path.join(dirname, readdir[j]);
if (fs.lstatSync(dirfile).isDirectory()) {
helper.objectPush(this._watched, dirname, dirfile + path.sep);
}
}
}
return this;
};
Expand Down
12 changes: 11 additions & 1 deletion test/add_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ exports.add = {
done();
},
addToWatched: function (test) {
test.expect(1);
test.expect(2);
var oldreaddirSync = fs.readdirSync;
var readdirCount = 0;
fs.readdirSync = function() {
readdirCount++;
return oldreaddirSync.apply(this, arguments);
};
var files = [
'Project (LO)/',
'Project (LO)/one.js',
Expand All @@ -31,10 +37,14 @@ exports.add = {
'nested/': ['sub/', 'sub2/', 'one.js', 'three.js'],
'nested/sub/': ['two.js'],
};

var gaze = new Gaze('addnothingtowatch');
gaze._addToWatched(files);
var result = gaze.relative(null, true);
test.deepEqual(sortobj(result), sortobj(expected));
test.strictEqual(readdirCount, 4);

fs.readdirSync = oldreaddirSync;
gaze.on('end', test.done);
gaze.close();
},
Expand Down

0 comments on commit aac0c51

Please sign in to comment.