Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improve watch to use directories for fewer listeners

  • Loading branch information...
commit 6b8ea0395ff5e6d6f9ed9be74b639d55f03e46fc 1 parent 5ecc5a7
@mde authored
Showing with 30 additions and 10 deletions.
  1. +30 −10 lib/file.js
View
40 lib/file.js
@@ -18,7 +18,8 @@
var fs = require('fs')
, path = require('path')
- , DEFAULT_FILE_PAT = /\.(js|coffee|css|less|scss)$/
+ , DEFAULT_INCLUDE_PAT = /\.(js|coffee|css|less|scss)$/
+ , DEFAULT_EXCLUDE_PAT = /\.git|node_modules/
, logger;
var logger = new (function () {
@@ -178,30 +179,49 @@ var fileUtils = new (function () {
// Recursively watch files with a callback
_watch = function () {
var args = Array.prototype.slice.call(arguments)
- , path
+ , filePath
, opts
, callback
- , pat;
+ , inclPat
+ , exclPat
+ , createWatcher;
- path = args.shift();
+ filePath = args.shift();
callback = args.pop();
opts = args.pop() || {};
- pat = opts.pattern || DEFAULT_FILE_PAT;
+ inclPat = opts.includePattern || DEFAULT_INCLUDE_PAT;
+ exclPat = opts.excludePattern || DEFAULT_EXCLUDE_PAT;
- fs.stat(path, function (err, stats) {
+ opts.level = opts.level || 1;
+
+ createWatcher = function (watchPath) {
+ if (!exclPat.test(watchPath)) {
+ fs.watch(watchPath, function (ev, p) {
+ if (inclPat.test(p) && !exclPat.test(p)) {
+ callback(path.join(watchPath, p));
+ }
+ });
+ }
+ };
+
+ fs.stat(filePath, function (err, stats) {
if (err) {
return false;
}
- if (stats.isFile() && pat.test(path)) {
- fs.watchFile(path, callback);
+ // Watch files at the top level
+ if (stats.isFile() && opts.level == 1) {
+ createWatcher(filePath);
+ opts.level++;
}
else if (stats.isDirectory()) {
- fs.readdir(path, function (err, files) {
+ createWatcher(filePath);
+ opts.level++;
+ fs.readdir(filePath, function (err, files) {
if (err) {
return log.fatal(err);
}
for (var f in files) {
- _watch(path + '/' + files[f], opts, callback);
+ _watch(path.join(filePath, files[f]), opts, callback);
}
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.