Skip to content

Commit

Permalink
expose method from file-watcher plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Apr 11, 2015
1 parent 9907110 commit cdbda2f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
34 changes: 29 additions & 5 deletions lib/file-watcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

var _ = require("lodash");

/**
* Plugin interface
* @returns {*|function(this:exports)}
Expand Down Expand Up @@ -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);
}
});
}
Expand All @@ -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;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit cdbda2f

Please sign in to comment.