watch-glob
Version:
Watch glob patterns
91 lines (82 loc) • 2.74 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var Gaze, Vinyl, globule, path, watchGlob, _,
__slice = [].slice;
_ = require('lodash');
Gaze = require('gaze').Gaze;
path = require('path');
globule = require('globule');
Vinyl = require('vinyl');
watchGlob = function(patterns, options, addedCallback, removedCallback) {
var watcher;
watcher = new Gaze([], {
cwd: options != null ? options.cwd : void 0
});
setTimeout((function() {
return watcher.add(patterns);
}), options.delay);
watcher.on('all', function(evt, absoluteFilepath) {
var confirmMatch, relativeFilepath, valueForCallback;
relativeFilepath = path.relative(options.cwd, absoluteFilepath);
confirmMatch = globule.match(patterns, relativeFilepath);
if (confirmMatch.length === 0) {
return;
}
valueForCallback = (function() {
switch (options != null ? options.callbackArg : void 0) {
case 'absolute':
return absoluteFilepath;
case 'relative':
return relativeFilepath;
case 'vinyl':
return new Vinyl({
cwd: options.base,
path: absoluteFilepath
});
default:
return {
base: path.normalize(options.cwd),
path: path.normalize(absoluteFilepath),
relative: path.normalize(relativeFilepath)
};
}
})();
if (evt === 'added' || evt === 'changed' || evt === 'renamed') {
return addedCallback(valueForCallback);
} else if (evt === 'deleted') {
return removedCallback(valueForCallback);
}
});
return {
destroy: function() {
return watcher.close.apply(watcher);
},
watched: function() {
return watcher.watched.apply(watcher);
}
};
};
module.exports = function() {
var addedCallback, args, callbacksIndex, options, patterns, removedCallback;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
patterns = _.isArray(args[0]) ? args[0] : [args[0]];
callbacksIndex = null;
if (_.isPlainObject(args[1])) {
options = args[1];
callbacksIndex = 2;
} else if (_.isString(args[1])) {
options = {
cwd: args[1]
};
callbacksIndex = 2;
} else {
options = {};
callbacksIndex = 1;
}
options.cwd = options.cwd || process.cwd();
addedCallback = args[callbacksIndex] || (function() {});
removedCallback = args[callbacksIndex + 1] || (function() {});
options.delay = options.delay != null ? options.delay : 2000;
return watchGlob(patterns, options, addedCallback, removedCallback);
};
}).call(this);