diff --git a/src/watcher/index.coffee b/src/watcher/index.coffee index 5406571..653b788 100644 --- a/src/watcher/index.coffee +++ b/src/watcher/index.coffee @@ -26,6 +26,15 @@ class Watcher extends EventEmitter module ?= new modules[@_module] @[name] = method for name, method of module + emit: (event, arg1) -> + data = if arguments.length == 2 then [arg1] else [].slice.call(arguments, 1) + args = [event].concat(data) + EventEmitter.prototype.emit.apply(this, args) + if event == 'add' || event == 'add:dir' || event == 'add:file' || event == 'rem' || + event == 'rem:file' || event == 'rem:dir' || event == 'change' || + event == 'change:dir' || event == 'change:file' + EventEmitter.prototype.emit.apply(this, ['all'].concat(args)) + _debug: (name) -> debug = require('debug')("#{name}:watcher") @on 'add:file', (path) -> diff --git a/test/watcher_test.coffee b/test/watcher_test.coffee index 4d02dad..fbaf620 100644 --- a/test/watcher_test.coffee +++ b/test/watcher_test.coffee @@ -76,6 +76,65 @@ describe 'Watcher', -> it '`interval` - option to pass to watchFile' it '`module` - option to select the watch module' + describe 'all', -> + before -> + @spys = + 'all': sinon.spy() + 'add': sinon.spy() + 'rem': sinon.spy() + 'change': sinon.spy() + 'error': sinon.spy() + @watcher = new Watcher + for own key, value of @spys + @watcher.on key, value + mkdirSync fixture 'a' + mkdirSync fixture 'b' + writeFileSync fixture 'a', '1.js' + @watcher.add fixture 'a' + @watcher.add fixture 'b' + @watcher.add fixture 'a', '1.js' + + after -> + @watcher.close() + for own key, value of @spys + @watcher.removeListener key, value + delete @spys[key] + delete @watcher + rmdirSync fixture 'e' + + beforeEach (done) -> + delay done + + it 'Should emit `all` on new directories', -> + @spys['add'].should.have.been.called + @spys['all'].should.have.been.called + + it 'Should emit `all` on new files', -> + @spys['add'].should.have.been.called + @spys['all'].should.have.been.called + + # FIXME no rem event sent for rmdir + # it 'Should emit `all` on removed directories', (done) -> + # rmdirSync fixture 'b' + # delay => + # @spys['rem'].should.have.been.called + # @spys['all'].should.have.been.called + # done() + + it 'Should emit `all` on removed files', (done) -> + unlinkSync (fixture 'a', '1.js') + delay => + @spys['rem'].should.have.been.called + @spys['all'].should.have.been.called + done() + + it 'Should emit `all` when directories renamed', (done) -> + renameSync (fixture 'a'), (fixture 'e') + delay => + @spys['change'].should.have.been.called + @spys['all'].should.have.been.called + done() + describe 'add', -> before -> @spys =