diff --git a/README.md b/README.md index 9ef88ce..c17dd00 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,6 @@ wt [download-image]: https://img.shields.io/npm/dm/wt.svg?style=flat-square [download-url]: https://npmjs.org/package/wt -![logo](https://raw.github.com/node-modules/wt/master/logo.png) - wt: Simple dir watcher, including all subdirectories, support events and multi dirs: * `all`: every change event diff --git a/index.js b/index.js index 5316fd4..ca04774 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,11 @@ /**! * wt - index.js * - * Copyright(c) fengmk2 and other contributors. + * Copyright(c) node-modules and other contributors. * MIT Licensed * * Authors: - * fengmk2 (http://fengmk2.github.com) + * fengmk2 (http://fengmk2.com) */ 'use strict'; @@ -20,7 +20,6 @@ var fs = require('fs'); var EventEmitter = require('events').EventEmitter; var util = require('util'); var ndir = require('ndir'); -var pedding = require('pedding'); module.exports = Watcher; @@ -64,11 +63,16 @@ Watcher.watch = function (dirs, options, done) { done = function() {}; } - var len = Array.isArray(dirs) ? dirs.length : 1; + var count = Array.isArray(dirs) ? dirs.length : 1; return new Watcher(options) .watch(dirs) .once('error', done) - .on('watch', pedding(len, done).bind(null, null)); + .on('watch', function () { + count--; + if (count === 0) { + done(); + } + }); }; util.inherits(Watcher, EventEmitter); diff --git a/logo.png b/logo.png deleted file mode 100644 index 4aa6b36..0000000 Binary files a/logo.png and /dev/null differ diff --git a/test/index.test.js b/test/index.test.js index 616e6cc..d0dcf0e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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) { @@ -69,6 +70,20 @@ describe('index.test.js', function () { 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'); @@ -175,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(); + }); + }); });