vigil
Version:
Simple, efficient file watching.
84 lines (76 loc) • 2.05 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var debounce, fs, parseArgs, path, ref, vm, walk;
fs = require('fs');
path = require('path');
vm = require('./vm');
walk = require('./walk');
ref = require('./utils'), debounce = ref.debounce, parseArgs = ref.parseArgs;
module.exports = parseArgs(function(basePath, opts, cb) {
var excluded, modules, relative, watch, watching;
relative = opts.relative, excluded = opts.excluded;
if (opts.patch == null) {
opts.patch = false;
}
if (opts.recurse == null) {
opts.recurse = true;
}
if (opts.watchSymlink == null) {
opts.watchSymlink = false;
}
modules = {};
watching = {};
watch = function(dir, isModule) {
var onchange;
if (isModule) {
return modules[dir] = true;
}
if (watching[dir]) {
return;
}
onchange = function(event, filename) {
filename = path.join(dir, filename);
if (excluded(filename)) {
return;
}
return fs.stat(filename, function(err, stats) {
var ref1;
if ((err != null) || (stats == null)) {
return;
}
if (stats.isDirectory()) {
return watch(filename);
} else if (stats.isSymbolicLink() && opts.watchSymlink) {
return fs.readLink(filename, function(err, realPath) {
return watch(path.dirname(realPath));
});
} else {
return cb(filename, stats, (ref1 = modules[filename]) != null ? ref1 : false);
}
});
};
return watching[dir] = fs.watch(dir, debounce(500, onchange));
};
watch(basePath);
if (opts.recurse) {
walk(basePath, opts, function(filename, stats) {
if (excluded(filename)) {
return;
}
if (stats.isDirectory()) {
return watch(filename);
}
});
}
if (opts.patch) {
vm(function(filename, stats) {
if (excluded(filename)) {
return;
}
if (stats.isDirectory()) {
return watch(filename, true);
}
});
}
return watch;
});
//# sourceMappingURL=watch.js.map