2014-05-23 18:14:06 +00:00
|
|
|
var EventEmitter = require('../lib/eventemitter.js');
|
2014-07-14 19:51:40 +00:00
|
|
|
var Path = require('./path.js');
|
2014-05-23 18:14:06 +00:00
|
|
|
var Intercom = require('../lib/intercom.js');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FSWatcher based on node.js' FSWatcher
|
|
|
|
* see https://github.com/joyent/node/blob/master/lib/fs.js
|
|
|
|
*/
|
|
|
|
function FSWatcher() {
|
|
|
|
EventEmitter.call(this);
|
|
|
|
var self = this;
|
|
|
|
var recursive = false;
|
2014-07-14 20:46:52 +00:00
|
|
|
var recursivePathPrefix;
|
2014-05-23 18:14:06 +00:00
|
|
|
var filename;
|
|
|
|
|
|
|
|
function onchange(path) {
|
2014-07-14 20:46:52 +00:00
|
|
|
// Watch for exact filename, or parent path when recursive is true.
|
|
|
|
if(filename === path || (recursive && path.indexOf(recursivePathPrefix) === 0)) {
|
2014-05-23 18:14:06 +00:00
|
|
|
self.trigger('change', 'change', path);
|
2014-03-08 01:21:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
// We support, but ignore the second arg, which node.js uses.
|
|
|
|
self.start = function(filename_, persistent_, recursive_) {
|
|
|
|
// Bail if we've already started (and therefore have a filename);
|
|
|
|
if(filename) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-14 19:51:40 +00:00
|
|
|
if(Path.isNull(filename_)) {
|
2014-05-23 18:14:06 +00:00
|
|
|
throw new Error('Path must be a string without null bytes.');
|
|
|
|
}
|
2014-07-14 19:51:40 +00:00
|
|
|
|
2014-05-23 18:14:06 +00:00
|
|
|
// TODO: get realpath for symlinks on filename...
|
2014-07-14 19:51:40 +00:00
|
|
|
|
|
|
|
// Filer's Path.normalize strips trailing slashes, which we use here.
|
|
|
|
// See https://github.com/js-platform/filer/issues/105
|
|
|
|
filename = Path.normalize(filename_);
|
2014-05-23 18:14:06 +00:00
|
|
|
|
|
|
|
// Whether to watch beneath this path or not
|
|
|
|
recursive = recursive_ === true;
|
2014-07-14 20:46:52 +00:00
|
|
|
// If recursive, construct a path prefix portion for comparisons later
|
|
|
|
// (i.e., '/path' becomes '/path/' so we can search within a filename for the
|
|
|
|
// prefix). We also take care to allow for '/' on its own.
|
|
|
|
if(recursive) {
|
|
|
|
recursivePathPrefix = filename === '/' ? '/' : filename + '/';
|
|
|
|
}
|
2014-05-23 18:14:06 +00:00
|
|
|
|
|
|
|
var intercom = Intercom.getInstance();
|
|
|
|
intercom.on('change', onchange);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.close = function() {
|
|
|
|
var intercom = Intercom.getInstance();
|
|
|
|
intercom.off('change', onchange);
|
|
|
|
self.removeAllListeners('change');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
FSWatcher.prototype = new EventEmitter();
|
|
|
|
FSWatcher.prototype.constructor = FSWatcher;
|
|
|
|
|
|
|
|
module.exports = FSWatcher;
|