Skip to content

Commit

Permalink
Fix plugin option merging
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane authored and Shane Osbourne committed Apr 9, 2015
1 parent 27d9850 commit 5a66cbd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 67 deletions.
88 changes: 29 additions & 59 deletions lib/file-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,75 +19,45 @@ module.exports.plugin = function (options, emitter) {
}

var globs = options.get("files");
console.log(globs);

return globs.reduce(function (map, glob, namespace) {

//console.log(glob);
/**
* Default CB when not given
* @param event
* @param path
*/
//var fn = function (event, path) {
// emitter.emit("file:changed", {
// event: event,
// path: path,
// namespace: namespace
// });
//};
var fn = function (event, path) {
emitter.emit("file:changed", {
event: event,
path: path,
namespace: namespace
});
};

/**
* Handle lists
*/
//if (Immutable.List.isList(glob)) {
// var watcher = getWatcher(glob.toJS(), watchOptions, fn);
// if (!map[namespace]) {
// map[namespace] = [watcher];
// } else {
// map[namespace].push(watcher);
// }
//}
var jsItem = glob.toJS();

/**
* Handle maps
*/
//if (Immutable.Map.isMap(glob)) {
//
// glob.forEach(function (value, key) {
//
// if (Immutable.List.isList(value) && value.size) {
//
// value.forEach(function (item) {
// var watcher = getWatcher(item.get("match").toJS(), watchOptions, item.get("fn"));
// if (!map[namespace]) {
// map[namespace] = [watcher];
// } else {
// map[namespace].push(watcher);
// }
// });
// } else {
//
// if (isString(key) && key !== "multi") {
//
// var localfn = fn;
//
// if (isFunction(value)) {
// localfn = value;
// }
//
// var watcher = getWatcher(key, watchOptions, localfn);
//
// if (!map[namespace]) {
// map[namespace] = [watcher];
// } else {
// map[namespace].push(watcher);
// }
// }
// }
//
// });
//}
if (jsItem.globs.length) {
if (!map[namespace]) {
map[namespace] = {
watchers: [getWatcher(jsItem.globs, watchOptions, fn)]
};
} else {
map[namespace].watchers.push(getWatcher(jsItem.globs, watchOptions, fn));
}
}

if (jsItem.objs.length) {
jsItem.objs.forEach(function (item) {
if (!map[namespace]) {
map[namespace] = {
watchers: [getWatcher(item.match, watchOptions, item.fn)]
};
} else {
map[namespace].watchers.push(getWatcher(item.match, watchOptions, item.fn));
}
});
}

return map;

Expand Down
9 changes: 1 addition & 8 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = {

var opts;

if (pluginOptions) { // todo: use immutable data for plugin options also
if (pluginOptions) {
opts = Immutable.fromJS(pluginOptions);
opts.forEach(function (value, key) {
var files = value.get("files");
Expand All @@ -89,13 +89,6 @@ module.exports = {
}
}
});
//console.log(pluginOptions);
//_.each(pluginOptions, function (value, key) {

//if (value && value.files) {
// initial[key] = utils.makeList(value.files);
//}
//});
}

return initial;
Expand Down
36 changes: 36 additions & 0 deletions test/specs/hooks/files.watch.hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,40 @@ describe("files:watch hook", function () {
assert.equal(out.plugin1.globs[0], "*.hbs");
assert.equal(out.plugin1.objs[0].fn, cb);
});

it("should string globs + objects as file watching patterns", function () {

var cb = function (event, file) {
console.log(file);
};

var imm = merge({
files: [
"*.html",
{
match: "*.css",
fn: cb
}
]
});

var pluginOptions = {
"plugin1": {
files: [
"*.hbs",
{
match: "*.less",
fn: cb
}
]
}
};

var out = hook([], imm.get("files"), pluginOptions);
imm = imm.set("files", out);

var watchers = require("../../../lib/file-watcher").plugin(imm, {});
assert.equal(watchers.core.watchers.length, 2);
assert.equal(watchers.plugin1.watchers.length, 2);
});
});

0 comments on commit 5a66cbd

Please sign in to comment.