Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to chokidar #13

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this option needs documentation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Original file line number Diff line number Diff line change
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