From cdbda2f391542bc59699a051298b892935f76b51 Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Sat, 11 Apr 2015 18:59:38 +0100 Subject: [PATCH] expose method from file-watcher plugin --- lib/file-watcher.js | 34 +++++++++++++++++++++++++++++----- package.json | 4 ++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/file-watcher.js b/lib/file-watcher.js index 48c686a4b..e4a09c182 100644 --- a/lib/file-watcher.js +++ b/lib/file-watcher.js @@ -1,5 +1,7 @@ "use strict"; +var _ = require("lodash"); + /** * Plugin interface * @returns {*|function(this:exports)} @@ -33,18 +35,19 @@ module.exports.plugin = function (options, emitter) { if (jsItem.globs.length) { map[namespace] = { - watchers: [getWatcher(jsItem.globs, defaultWatchOptions, fn)] + watchers: [watch(jsItem.globs, defaultWatchOptions, fn)] }; } if (jsItem.objs.length) { jsItem.objs.forEach(function (item) { + var watcher = watch(item.match, item.options || defaultWatchOptions, item.fn); if (!map[namespace]) { map[namespace] = { - watchers: [getWatcher(item.match, item.options || defaultWatchOptions, item.fn)] + watchers: [watcher] }; } else { - map[namespace].watchers.push(getWatcher(item.match, item.options || defaultWatchOptions, item.fn)); + map[namespace].watchers.push(watcher); } }); } @@ -54,6 +57,27 @@ module.exports.plugin = function (options, emitter) { }, {}); }; -function getWatcher (patterns, opts, fn) { - return require("chokidar").watch(patterns, opts).on("all", fn); +/** + * @param patterns + * @param opts + * @param cb + * @returns {*} + */ +function watch (patterns, opts, cb) { + + if (typeof opts === "function") { + cb = opts; + opts = {}; + } + + var watcher = require("chokidar") + .watch(patterns, opts); + + if (_.isFunction(cb)) { + watcher.on("all", cb); + } + + return watcher; } + +module.exports.watch = watch; diff --git a/package.json b/package.json index 757277cc6..5a7ac57f4 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "async-each-series": "^0.1.1", "browser-sync-client": "^1.0.1", "browser-sync-ui": "^0.5.6", - "chokidar": "^1.0.0", + "chokidar": "^1.0.1", "connect": "^3.3.5", "dev-ip": "^1.0.1", "easy-extender": "^2.3.0", @@ -77,7 +77,7 @@ "istanbul": "^0.3.2", "istanbul-coveralls": "^1.0.0", "mocha": "*", - "q": "^1.1.2", + "q": "^1.2.0", "request": "^2.55.0", "sinon": "^1.12.1", "slugify": "^0.1.0",