Skip to content

Commit

Permalink
Merge a9a5fb3 into ecdbcc5
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed May 6, 2015
2 parents ecdbcc5 + a9a5fb3 commit 4801f50
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 164 deletions.
55 changes: 28 additions & 27 deletions index.js
Expand Up @@ -20,6 +20,7 @@ var fs = require('fs');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var ndir = require('ndir');
var pedding = require('pedding');

module.exports = Watcher;

Expand All @@ -31,18 +32,12 @@ module.exports = Watcher;
* - {Boolean} [ignoreHidden] ignore hidden file or not, default is `true`
* @param {Function} [done], watch all dirs done callback.
*/
function Watcher(dirs, options, done) {
function Watcher(options) {
// http://nodejs.org/dist/v0.11.12/docs/api/fs.html#fs_caveats
// The recursive option is currently supported on OS X.
// Only FSEvents supports this type of file watching
// so it is unlikely any additional platforms will be added soon.

if (typeof options === 'function') {
// Watcher(dirs, done);
done = options;
options = null;
}

options = options || {};
if (options.ignoreHidden === undefined || options.ignoreHidden === null) {
options.ignoreHidden = true;
Expand All @@ -54,34 +49,38 @@ function Watcher(dirs, options, done) {
recursive: false, // so we dont use this features
};

if (typeof dirs === 'string') {
dirs = [dirs];
}

this._watchers = {};
dirs.forEach(this.watch.bind(this));

var index = 0;
var that = this;
dirs.forEach(function (dir) {
that.once('watch-' + dir, function () {
if (++index === dirs.length) {
debug('watch %j ready', dirs);
done && done();
}
});
});
}

Watcher.watch = function (dirs, options, done) {
return new Watcher(dirs, options, done);
// watch(dirs, done);
if (typeof options === 'function') {
done = options;
options = null;
}

// watch(dirs);
if (!done) {
done = function() {};
}

var len = Array.isArray(dirs) ? dirs.length : 1;
return new Watcher(options)
.watch(dirs)
.once('error', done)
.on('watch', pedding(len, done).bind(null, null));
};

util.inherits(Watcher, EventEmitter);

var proto = Watcher.prototype;

proto.watch = function (dir) {
if (Array.isArray(dir)) {
dir.forEach(this.watch.bind(this));
return this;
}

var watchers = this._watchers;
var that = this;
debug('walking %s...', dir);
Expand All @@ -105,11 +104,14 @@ proto.watch = function (dir) {
watchers[dirpath] = watcher;
watcher.once('error', that._onWatcherError.bind(that, dirpath));
}).on('error', function (err) {
that.emit('watch-error-' + dir, err);
err.dir = dir;
that.emit('watch-error', err);
}).on('end', function () {
debug('watch %s done', dir);
that.emit('watch-' + dir);
debug('now watching %s', Object.keys(that._watchers));
that.emit('watch', dir);
});
return this;
};

proto.close = function () {
Expand Down Expand Up @@ -172,7 +174,6 @@ proto._handle = function (root, event, name) {
that.watch(info.path);
}
}

that.emit('all', info);
if (info.remove) {
debug('remove %s', fullpath);
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -19,7 +19,6 @@
"devDependencies": {
"autod": "*",
"contributors": "*",
"cov": "*",
"istanbul": "*",
"jshint": "*",
"mocha": "*",
Expand Down
44 changes: 39 additions & 5 deletions test/index.test.js
Expand Up @@ -26,11 +26,12 @@ describe('index.test.js', function () {
this.watcher = wt.watch(fixtures, done);
});

afterEach(function () {
afterEach(function (done) {
try {
fs.rmdirSync(path.join(fixtures, '.createdir'));
} catch (err) {}
this.watcher.close();
setTimeout(done, 100);
});

it('should watch file change', function (done) {
Expand All @@ -52,8 +53,7 @@ describe('index.test.js', function () {
var filepath = path.join(fixtures, '.file.txt.swp');
fs.writeFile(filepath, 'vim tmp file\n', done);

var lastpath = null;
this.watcher.on('file', function (info) {
this.watcher.on('file', function () {
throw new Error('should not run this');
});
setTimeout(done, 200);
Expand All @@ -64,13 +64,26 @@ describe('index.test.js', function () {
var dirpath = path.join(fixtures, '.createdir');
fs.mkdir(dirpath, done);

var lastpath = null;
this.watcher.on('dir', function (info) {
this.watcher.on('dir', function () {
throw new Error('should not run this');
});
setTimeout(done, 200);
});

it('should not ignore hidden dir change with ignoreHidden option', function (done) {
done = pedding(2, done);

this.watcher.close();
this.watcher = wt.watch(fixtures, {ignoreHidden: false}, function() {
var dirpath = path.join(fixtures, '.createdir');
fs.mkdir(dirpath, done);

this.watcher.on('dir', function () {
done();
});
}.bind(this));
});

it('should watch subdir file change', function (done) {
done = pedding(2, done);
var filepath = path.join(fixtures, 'subdir', 'subfoo.txt');
Expand Down Expand Up @@ -177,4 +190,25 @@ describe('index.test.js', function () {
done();
});
});

it('should emit watch event twice', function(done) {
done = pedding(2, done);

this.watcher.close();
this.watcher = wt.watch([
path.join(fixtures, 'subdir'),
path.join(fixtures, 'subdir2')
]).on('watch', done.bind(null, null));
});

it('should emit watch-error event', function(done) {
var filepath = path.join(fixtures, 'not-exist');

this.watcher.close();
this.watcher = wt.watch(filepath)
.on('watch-error', function(err) {
err.dir.should.eql(filepath);
done();
});
});
});

0 comments on commit 4801f50

Please sign in to comment.