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

remove the change event object being passed to the callback #18

Merged
merged 5 commits into from
Mar 22, 2015
Merged
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
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
node_js:
- "0.9"
- "0.10"
- "0.12"
- "iojs"
after_script:
- npm run coveralls
- npm run coveralls
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Information

<table>
<tr>
<tr>
<td>Package</td><td>glob-watcher</td>
</tr>
<tr>
Expand All @@ -12,7 +12,7 @@
</tr>
<tr>
<td>Node Version</td>
<td>>= 0.9</td>
<td>>= 0.10</td>
</tr>
</table>

Expand All @@ -22,18 +22,22 @@
var watch = require('glob-watcher');

// callback interface
watch(["./*.js", "!./something.js"], function(evt){
// evt has what file changed and all that jazz
watch(['./*.js', '!./something.js'], function(){
// this function will be called each time a globbed
// file is changed

// if you need access to the `evt` object, listen
// for the `change` event (see below)
});

// EE interface
var watcher = watch(["./*.js", "!./something.js"]);
var watcher = watch(['./*.js', '!./something.js']);
watcher.on('change', function(evt) {
// evt has what file changed and all that jazz
});

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


Expand Down
30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
var gaze = require('gaze');
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();
});
}
}

module.exports = function(glob, opts, cb) {
var out = new EventEmitter();

Expand All @@ -9,15 +21,7 @@ module.exports = function(glob, opts, cb) {
opts = {};
}

var watcher = gaze(glob, opts, 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(outEvt);
});
});
var watcher = gaze(glob, opts, onWatch(out, cb));

watcher.on('end', out.emit.bind(out, 'end'));
watcher.on('error', out.emit.bind(out, 'error'));
Expand All @@ -27,11 +31,11 @@ module.exports = function(glob, opts, cb) {
out.end = function(){
return watcher.close();
};
out.add = function(){
return watcher.add.apply(watcher, arguments);
out.add = function(glob, cb){
return watcher.add(glob, onWatch(out, cb));
};
out.remove = function(){
return watcher.remove.apply(watcher, arguments);
out.remove = function(glob){
return watcher.remove(glob);
};
out._watcher = watcher;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
},
"engines": {
"node": ">= 0.9"
"node": ">= 0.10"
},
"licenses": [
{
Expand Down
56 changes: 48 additions & 8 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,14 @@ describe('glob-watcher', function() {
});
});

it('should return a valid file struct via callback', function(done) {
it('should not pass any args via callback', function(done) {
var expectedName = path.join(__dirname, './fixtures/stuff/test.coffee');
var fname = path.join(__dirname, './fixtures/**/test.coffee');
mkdirp.sync(path.dirname(expectedName));
fs.writeFileSync(expectedName, 'testing');

var watcher = watch(fname, function(evt) {
should.exist(evt);
should.exist(evt.path);
should.exist(evt.type);
evt.type.should.equal('changed');
evt.path.should.equal(expectedName);
var watcher = watch(fname, function() {
arguments.length.should.equal(0);
watcher.end();
});

Expand All @@ -69,6 +65,50 @@ describe('glob-watcher', function() {
}, timeout);
});

it('should call the callback registered with `add`', function(done) {
var expectedName = path.join(__dirname, './fixtures/stuff/test.coffee');
var fname = path.join(__dirname, './fixtures/**/test.coffee');
mkdirp.sync(path.dirname(expectedName));
fs.writeFileSync(expectedName, 'testing');

var watcher = watch();
watcher.add(fname, function() {
arguments.length.should.equal(0);
watcher.end();
});

watcher.once('end', function(){
rimraf.sync(expectedName);
done();
});
setTimeout(function(){
fs.writeFileSync(expectedName, 'test test');
}, timeout);
});

it('should not call a callback when filepath removed with `remove`', function(done) {
var expectedName = path.join(__dirname, './fixtures/stuff/test.coffee');
var fname = path.join(__dirname, './fixtures/**/test.coffee');
mkdirp.sync(path.dirname(expectedName));
fs.writeFileSync(expectedName, 'testing');

var watcher = watch();
watcher.add(fname, function() {
// if we get here, that's bad and should fail
true.should.equal(false);
watcher.end();
});
watcher.remove(expectedName);

setTimeout(function(){
fs.writeFileSync(expectedName, 'test test');
}, timeout);
setTimeout(function(){
rimraf.sync(expectedName);
done();
}, timeout * 3);
});

it('should not return a non-matching file struct via callback', function(done) {
var expectedName = path.join(__dirname, './fixtures/test123.coffee');
var fname = path.join(__dirname, './fixtures/**/test.coffee');
Expand All @@ -88,4 +128,4 @@ describe('glob-watcher', function() {
done();
}, timeout * 2);
});
});
});