Skip to content

Commit

Permalink
Convert relative() to async. Ref GH-76.
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed Apr 9, 2014
1 parent 46ceed8 commit 6d65161
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 89 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ gaze('**/*.js', function(err, watcher) {
});

// Get watched files with relative paths
console.log(this.relative());
this.relative(function(err, files) {
console.log(files);
});
});

// Also accepts an array of patterns
Expand Down Expand Up @@ -140,10 +142,12 @@ var gaze = new Gaze(pattern, options, callback);
* `add(patterns, callback)` Adds file(s) patterns to be watched.
* `remove(filepath)` removes a file or directory from being watched. Does not
recurse directories.
* `watched()` Returns the currently watched files.
* `relative([dir, unixify])` Returns the currently watched files with relative paths.
* `watched([callback])` Returns the currently watched files.
* `callback` {function} Calls with `function(err, files)`.
* `relative([dir, unixify, callback])` Returns the currently watched files with relative paths.
* `dir` {string} Only return relative files for this directory.
* `unixify` {boolean} Return paths with `/` instead of `\\` if on Windows.
* `callback` {function} Calls with `function(err, files)`.

## FAQs

Expand Down
9 changes: 5 additions & 4 deletions benchmarks/relative.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ b.table.setHeading('files', 'ms').setAlign(0, 2).setAlign(1, 2);
b.run(function(num, done) {
gaze('**/*', {cwd: b.tmpDir, maxListeners:0}, function(err, watcher) {
b.start();
var files = this.relative('.');
b.log(num, b.end());
watcher.on('end', done);
watcher.close();
this.relative('.', function(err, files) {
b.log(num, b.end());
watcher.on('end', done);
watcher.close();
});
});
}, function() {
process.exit();
Expand Down
38 changes: 27 additions & 11 deletions lib/gaze.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports.Gaze = Gaze;
Gaze.prototype.emit = function() {
var self = this;
var args = arguments;
//console.log('GAZE EMIT', arguments)

var e = args[0];
var filepath = args[1];
Expand Down Expand Up @@ -182,6 +183,7 @@ Gaze.prototype.close = function(_reset) {
// Add file patterns to be watched
Gaze.prototype.add = function(files, done) {
var self = this;
//console.log('ADD', files);
if (typeof files === 'string') { files = [files]; }
this._patterns = helper.unique.apply(null, [this._patterns, files]);
files = globule.find(this._patterns, this.options);
Expand Down Expand Up @@ -294,19 +296,33 @@ Gaze.prototype.remove = function(file) {
};

// Return watched files
Gaze.prototype.watched = function() {
return helper.flatToTree(platform.getWatchedPaths(), this.options.cwd, false);
Gaze.prototype.watched = function(done) {
done = nextback(done || function() {});
helper.flatToTree(platform.getWatchedPaths(), this.options.cwd, false, done);
return this;
};

// Returns `watched` files with relative paths to cwd
Gaze.prototype.relative = function(dir, unixify) {
var relative = helper.flatToTree(platform.getWatchedPaths(), this.options.cwd, true, unixify);
if (dir) {
if (unixify) {
dir = helper.unixifyPathSep(dir);
}
// Better guess what to return for backwards compatibility
return relative[dir] || relative[dir + (unixify ? '/' : path.sep)] || [];
Gaze.prototype.relative = function(dir, unixify, done) {
if (typeof dir === 'function') {
done = dir;
dir = null;
unixify = false;
}
return relative;
if (typeof unixify === 'function') {
done = unixify;
unixify = false;
}
done = nextback(done || function() {});
helper.flatToTree(platform.getWatchedPaths(), this.options.cwd, true, unixify, function(err, relative) {
if (dir) {
if (unixify) {
dir = helper.unixifyPathSep(dir);
}
// Better guess what to return for backwards compatibility
return done(null, relative[dir] || relative[dir + (unixify ? '/' : path.sep)] || []);
}
return done(null, relative);
});
return this;
};
22 changes: 17 additions & 5 deletions lib/gaze04.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,22 @@ Gaze.prototype.remove = function(file) {
};

// Return watched files
Gaze.prototype.watched = function() {
return this._watched;
Gaze.prototype.watched = function(done) {
done(null, this._watched);
return this;
};

// Returns `watched` files with relative paths to process.cwd()
Gaze.prototype.relative = function(dir, unixify) {
Gaze.prototype.relative = function(dir, unixify, done) {
if (typeof dir === 'function') {
done = dir;
dir = null;
unixify = false;
}
if (typeof unixify === 'function') {
done = unixify;
unixify = false;
}
var self = this;
var relative = Object.create(null);
var relDir, relFile, unixRelDir;
Expand All @@ -238,13 +248,15 @@ Gaze.prototype.relative = function(dir, unixify) {
if (unixify) {
relFile = helper.unixifyPathSep(relFile);
}
return relFile;
done(null, relFile);
return self;
});
});
if (dir && unixify) {
dir = helper.unixifyPathSep(dir);
}
return dir ? relative[dir] || [] : relative;
done(null, dir ? relative[dir] || [] : relative);
return self;
};

// Adds files and dirs to watched
Expand Down
18 changes: 10 additions & 8 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ helper.unixifyPathSep = function unixifyPathSep(filepath) {
};

// Converts a flat list of paths to the old style tree
helper.flatToTree = function flatToTree(files, cwd, relative, unixify) {
helper.flatToTree = function flatToTree(files, cwd, relative, unixify, done) {
cwd = helper.markDir(cwd);
var tree = Object.create(null);
for (var i = 0; i < files.length; i++) {
var filepath = files[i];

helper.forEachSeries(files, function(filepath, next) {
var parent = path.dirname(filepath) + path.sep;

// If parent outside cwd, ignore
if (path.relative(cwd, parent) === '..') {
continue;
return next();
}

// If we want relative paths
Expand All @@ -60,7 +60,7 @@ helper.flatToTree = function flatToTree(files, cwd, relative, unixify) {
} else {
parent = path.relative(cwd, parent) + path.sep;
}
filepath = path.relative(path.join(cwd, parent), files[i]) + (helper.isDir(files[i]) ? path.sep : '');
filepath = path.relative(path.join(cwd, parent), filepath) + (helper.isDir(filepath) ? path.sep : '');
}

// If we want to transform paths to unix seps
Expand All @@ -71,14 +71,16 @@ helper.flatToTree = function flatToTree(files, cwd, relative, unixify) {
}
}

if (!parent) continue;
if (!parent) { return next(); }

if (!Array.isArray(tree[parent])) {
tree[parent] = [];
}
tree[parent].push(filepath);
}
return tree;
next();
}, function() {
done(null, tree);
});
};

/**
Expand Down
25 changes: 17 additions & 8 deletions test/add_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ exports.add = {
addLater: function(test) {
test.expect(3);
new Gaze('sub/one.js', function(err, watcher) {
test.deepEqual(watcher.relative('sub'), ['one.js']);
watcher.add('sub/*.js', function() {
test.deepEqual(watcher.relative('sub'), ['one.js', 'two.js']);
watcher.on('changed', function(filepath) {
test.equal('two.js', path.basename(filepath));
watcher.on('end', test.done);
watcher.close();
watcher.on('changed', function(filepath) {
test.equal('two.js', path.basename(filepath));
watcher.on('end', test.done);
watcher.close();
});

function addLater() {
watcher.add('sub/*.js', function() {
watcher.relative('sub', function(err, result) {
test.deepEqual(result, ['one.js', 'two.js']);
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'two.js'), 'var two = true;');
});
});
fs.writeFileSync(path.resolve(__dirname, 'fixtures', 'sub', 'two.js'), 'var two = true;');
}

watcher.relative('sub', function(err, result) {
test.deepEqual(result, ['one.js']);
addLater();
});
});
},
Expand Down
28 changes: 17 additions & 11 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,34 @@ exports.api = {
newGaze: function(test) {
test.expect(2);
new gaze.Gaze('**/*', {}, function() {
var result = helper.sortobj(this.relative(null, true));
test.deepEqual(result['.'], ['Project (LO)/', 'nested/', 'one.js', 'sub/']);
test.deepEqual(result['sub/'], ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
this.relative(null, true, function(err, result) {
var result = helper.sortobj(result);
test.deepEqual(result['.'], ['Project (LO)/', 'nested/', 'one.js', 'sub/']);
test.deepEqual(result['sub/'], ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
}.bind(this));
});
},
func: function(test) {
test.expect(1);
var g = gaze('**/*', function(err, watcher) {
test.deepEqual(watcher.relative('sub', true), ['one.js', 'two.js']);
g.on('end', test.done);
g.close();
watcher.relative('sub', true, function(err, result) {
test.deepEqual(result, ['one.js', 'two.js']);
g.on('end', test.done);
g.close();
}.bind(this));
});
},
ready: function(test) {
test.expect(1);
var g = new gaze.Gaze('**/*');
g.on('ready', function(watcher) {
test.deepEqual(watcher.relative('sub', true), ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
watcher.relative('sub', true, function(err, result) {
test.deepEqual(result, ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
}.bind(this));
});
},
nomatch: function(test) {
Expand Down
14 changes: 8 additions & 6 deletions test/helper_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ exports.helper = {
'/Users/dude/www/sub/': ['/Users/dude/www/sub/one.js', '/Users/dude/www/sub/nested/'],
'/Users/dude/www/sub/nested/': ['/Users/dude/www/sub/nested/one.js'],
};
var actual = helper.flatToTree(files, cwd, false, true);
test.deepEqual(actual, expected);
test.done();
helper.flatToTree(files, cwd, false, true, function(err, actual) {
test.deepEqual(actual, expected);
test.done();
});
},
flatToTreeRelative: function(test) {
test.expect(1);
Expand All @@ -48,8 +49,9 @@ exports.helper = {
'sub/': ['one.js', 'nested/'],
'sub/nested/': ['one.js'],
};
var actual = helper.flatToTree(files, cwd, true, true);
test.deepEqual(actual, expected);
test.done();
helper.flatToTree(files, cwd, true, true, function(err, actual) {
test.deepEqual(actual, expected);
test.done();
});
},
};
57 changes: 31 additions & 26 deletions test/matching_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,53 @@ exports.matching = {
globAll: function(test) {
test.expect(2);
gaze('**/*', function() {
var result = this.relative(null, true);
test.deepEqual(result['.'], ['Project (LO)/', 'nested/', 'one.js', 'sub/']);
test.deepEqual(result['sub/'], ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
this.relative(null, true, function(err, result) {
test.deepEqual(result['.'], ['Project (LO)/', 'nested/', 'one.js', 'sub/']);
test.deepEqual(result['sub/'], ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
}.bind(this));
});
},
relativeDir: function(test) {
test.expect(1);
gaze('**/*', function() {
test.deepEqual(this.relative('sub', true), ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
this.relative('sub', true, function(err, result) {
test.deepEqual(result, ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
}.bind(this));
});
},
globArray: function(test) {
test.expect(2);
gaze(['*.js', 'sub/*.js'], function() {
var result = this.relative(null, true);
test.deepEqual(sortobj(result['.']), sortobj(['one.js', 'sub/']));
test.deepEqual(sortobj(result['sub/']), sortobj(['one.js', 'two.js']));
this.on('end', test.done);
this.close();
this.relative(null, true, function(err, result) {
test.deepEqual(sortobj(result['.']), sortobj(['one.js', 'sub/']));
test.deepEqual(sortobj(result['sub/']), sortobj(['one.js', 'two.js']));
this.on('end', test.done);
this.close();
}.bind(this));
});
},
globArrayDot: function(test) {
test.expect(1);
gaze(['./sub/*.js'], function() {
var result = this.relative(null, true);
test.deepEqual(result['sub/'], ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
this.relative(null, true, function(err, result) {
test.deepEqual(result['sub/'], ['one.js', 'two.js']);
this.on('end', test.done);
this.close();
}.bind(this));
});
},
oddName: function(test) {
test.expect(1);
gaze(['Project (LO)/*.js'], function() {
var result = this.relative(null, true);
test.deepEqual(result['Project (LO)/'], ['one.js']);
this.on('end', test.done);
this.close();
this.relative(null, true, function(err, result) {
test.deepEqual(result['Project (LO)/'], ['one.js']);
this.on('end', test.done);
this.close();
}.bind(this));
});
},
addedLater: function(test) {
Expand All @@ -82,11 +88,10 @@ exports.matching = {
gaze('**/*.js', function(err, watcher) {
watcher.on('all', function(status, filepath) {
var expect = expected.shift();
var result = watcher.relative(expect[0], true);
test.deepEqual(sortobj(result), sortobj(expect.slice(1)));
if (expected.length < 1) {
watcher.close();
}
watcher.relative(expect[0], true, function(err, result) {
test.deepEqual(sortobj(result), sortobj(expect.slice(1)));
if (expected.length < 1) { watcher.close(); }
}.bind(this));
});
grunt.file.write(path.join(fixtures, 'newfolder', 'added.js'), 'var added = true;');
setTimeout(function() {
Expand Down
Loading

0 comments on commit 6d65161

Please sign in to comment.