globwatcher
Version:
watch a set of files for changes (including create/delete) by glob patterns
169 lines (139 loc) • 4.26 kB
JavaScript
// Generated by CoffeeScript 1.6.2
(function() {
var FileWatcher, Q, Watch, events, fs, makePromise, path, util,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
events = require('events');
fs = require('fs');
path = require('path');
Q = require('q');
util = require('util');
makePromise = require("./make_promise").makePromise;
FileWatcher = (function() {
function FileWatcher(options_in) {
var k, options, v;
if (options_in == null) {
options_in = {};
}
options = {
period: 250,
persistent: true
};
for (k in options_in) {
v = options_in[k];
options[k] = v;
}
this.period = options.period;
this.persistent = options.persistent;
this.timer = null;
this.watches = {};
this.ongoing = null;
}
FileWatcher.prototype.close = function() {
this.watches = {};
if (this.timer != null) {
clearInterval(this.timer);
return this.timer = null;
}
};
FileWatcher.prototype.watch = function(filename, mtime, size) {
var watch,
_this = this;
if (mtime == null) {
mtime = null;
}
if (size == null) {
size = null;
}
filename = path.resolve(filename);
watch = this.watches[filename];
if (watch == null) {
watch = this.watches[filename] = new Watch(filename, mtime, size);
}
if (this.timer == null) {
this.timer = setInterval((function() {
return _this.check();
}), this.period);
if (!this.persistent) {
this.timer.unref();
}
}
return watch;
};
FileWatcher.prototype.unwatch = function(filename) {
var watch;
filename = path.resolve(filename);
watch = this.watches[filename];
if (watch != null) {
return delete this.watches[filename];
}
};
FileWatcher.prototype.watchFor = function(filename) {
return this.watches[path.resolve(filename)];
};
FileWatcher.prototype.check = function() {
var run,
_this = this;
run = function() {
var completion, filename, watch;
completion = Q.all((function() {
var _ref, _results;
_ref = this.watches;
_results = [];
for (filename in _ref) {
watch = _ref[filename];
_results.push(watch.check());
}
return _results;
}).call(_this));
return _this.ongoing = completion.then(function() {
return _this.ongoing = null;
});
};
if (this.ongoing != null) {
return this.ongoing = this.ongoing.then(run);
} else {
return run();
}
};
return FileWatcher;
})();
Watch = (function(_super) {
__extends(Watch, _super);
function Watch(filename, mtime, size) {
var e, stat;
this.filename = filename;
this.mtime = mtime;
this.size = size;
this.callbacks = [];
if (!((this.mtime != null) && (this.size != null))) {
try {
stat = fs.statSync(this.filename);
this.mtime = stat.mtime.getTime();
this.size = stat.size;
} catch (_error) {
e = _error;
}
}
}
Watch.prototype.check = function() {
var _this = this;
return makePromise(fs.stat)(this.filename).fail(function(error) {
return null;
}).then(function(stat) {
if ((_this.mtime != null) && (stat != null) && (_this.mtime !== stat.mtime.getTime() || _this.size !== stat.size)) {
_this.emit('changed', stat);
}
if (stat != null) {
_this.mtime = stat.mtime.getTime();
return _this.size = stat.size;
} else {
_this.mtime = null;
return _this.size = null;
}
});
};
return Watch;
})(events.EventEmitter);
exports.FileWatcher = FileWatcher;
}).call(this);