Skip to content

Commit

Permalink
Merge 26e6438 into 6520c5e
Browse files Browse the repository at this point in the history
  • Loading branch information
es128 committed Aug 18, 2015
2 parents 6520c5e + 26e6438 commit 96c127d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -38,8 +38,22 @@ watcher.on('change', function(evt) {

// add files after it has been created
watcher.add('./somefolder/somefile.js');

// stop watching certain files
watcher.remove('./somefolder/dontcare.*');

// stop watching entirely
watcher.close();

// options can be passed to the underlying watch lib (chokidar) as a second arg
watcher = watch('./*.js', {usePolling: true});
```

[Chokidar options reference](https://github.com/paulmillr/chokidar#api)

*Note:* glob-watcher overrides chokidar's default `ignoreInitial` setting to
`true` if you do not set it explicitly.


[npm-url]: https://npmjs.org/package/glob-watcher
[npm-image]: https://badge.fury.io/js/glob-watcher.png
Expand Down
81 changes: 59 additions & 22 deletions index.js
@@ -1,16 +1,11 @@
var gaze = require('gaze');
var chokidar = require('chokidar');
var anymatch = require('anymatch');
var EventEmitter = require('events').EventEmitter;

function onWatch(out, cb){
return function(err, rwatcher){
if (err) out.emit('error', err);
rwatcher.on('all', function(evt, path, old){
var outEvt = {type: evt, path: path};
if(old) outEvt.old = old;
out.emit('change', outEvt);
if(cb) cb();
});
}
var eventMap = {
add: 'added',
unlink: 'deleted',
change: 'changed'
}

module.exports = function(glob, opts, cb) {
Expand All @@ -21,22 +16,64 @@ module.exports = function(glob, opts, cb) {
opts = {};
}

var watcher = gaze(glob, opts, onWatch(out, cb));
opts = opts || {};

// overriding chokidar's default if user did not set it explicitly
if (opts.ignoreInitial == null) {
opts.ignoreInitial = true;
}

var watcher = chokidar.watch(glob, opts);

watcher.on('end', out.emit.bind(out, 'end'));
var nomatch = true;
var filteredCbs = [];

watcher.on('all', function(evt, path, stats){
// convert from chokidar event names to glob-watcher's original names
evt = eventMap[evt];
if (!evt) {
return;
}
nomatch = false;
var outEvt = {
type: evt,
path: path
};
if (stats) {
outEvt.stats = stats;
}
out.emit('change', outEvt);
filteredCbs.forEach(function(pair) {
if (pair.filter(path)) {
pair.cb();
}
});
cb && cb();
});
watcher.on('ready', function() {
if (nomatch) {
out.emit('nomatch');
}
out.emit('ready');
});
watcher.on('error', out.emit.bind(out, 'error'));
watcher.on('ready', out.emit.bind(out, 'ready'));
watcher.on('nomatch', out.emit.bind(out, 'nomatch'));

out.end = function(){
return watcher.close();
};
out.add = function(glob, cb){
return watcher.add(glob, onWatch(out, cb));
};
out.remove = function(glob){
return watcher.remove(glob);
if (cb) {
filteredCbs.push({
filter: anymatch(glob),
cb: cb
});
}
watcher.add(glob);
return watcher;
};
out.end = function() {
watcher.close();
out.emit('end');
return watcher;
}
out.remove = watcher.unwatch.bind(watcher);
out._watcher = watcher;

return out;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -11,7 +11,8 @@
"lib"
],
"dependencies": {
"gaze": "^0.5.1"
"anymatch": "^1.2.1",
"chokidar": "^1.0.0"
},
"devDependencies": {
"mocha": "^2.0.1",
Expand Down

0 comments on commit 96c127d

Please sign in to comment.