Skip to content

Commit ae29c48

Browse files
committed
update tests to match new plugin functionality
1 parent f252289 commit ae29c48

6 files changed

Lines changed: 110 additions & 84 deletions

File tree

lib/async.js

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
var _ = require("lodash");
44
var Immutable = require("immutable");
5-
var qs = require("qs");
65

76
var utils = require("./utils");
7+
var pluginUtils = require("./plugins");
88
var connectUtils = require("./connect-utils");
99

1010
module.exports = {
@@ -113,90 +113,27 @@ module.exports = {
113113

114114
var plugins = bs.options
115115
.get("plugins")
116-
.map(function (item) {
117-
if (typeof item === "string") {
118-
return getFromString(item);
119-
}
120-
if (Immutable.Map.isMap(item)) {
121-
122-
if (item.has("module")) {
123-
124-
var nameOrObj = item.get("module");
125-
var opts = item.get("opts");
126-
if (opts) {
127-
opts = opts.toJS();
128-
} else { opts = {} }
129-
130-
if (typeof nameOrObj === "string") {
131-
var plugin = getFromString(nameOrObj);
132-
plugin.opts = opts;
133-
return plugin;
134-
}
116+
.map(pluginUtils.resolvePlugin)
117+
.map(pluginUtils.requirePlugin);
135118

136-
if (Immutable.Map.isMap(nameOrObj)) {
137-
return {
138-
module: nameOrObj.toJS(),
139-
opts: opts
140-
}
141-
}
142-
}
119+
plugins
120+
.forEach(function (plugin) {
121+
if (plugin.errors && plugin.errors.length) {
122+
return logPluginError(plugin);
143123
}
144-
return {
145-
name: item,
146-
opts: {}
147-
}
148-
})
149-
.map(function (item) {
150-
if (!item.module) {
151-
item.module = require(item.name);
152-
}
153-
return item;
124+
bs.registerPlugin(plugin.module, plugin.options);
154125
});
155126

156-
console.log(plugins.toJS());
157-
158-
function getFromString(string) {
159-
var split = string.split('?');
160-
if (split.length > 1) {
161-
return {
162-
name: split[0],
163-
opts: qs.parse(split[1])
164-
}
165-
}
166-
return {
167-
name: split[0],
168-
opts: {}
169-
};
127+
function logPluginError (plugin) {
128+
bs.events.emit("config:error", {
129+
msg: [
130+
"Plugin: {cyan:'%s'} not found.", plugin.name,
131+
"\nPlease check for typos :)"
132+
]
133+
});
134+
utils.fail(true);
170135
}
171136

172-
//function createOneFromString(string) {
173-
//
174-
//}
175-
//bs.options.get("plugins").forEach(function (item) {
176-
//
177-
// if (_.isString(item)) {
178-
// loadPlugin(item);
179-
// }
180-
//
181-
// if (Immutable.Map.isMap(item)) {
182-
// if (item.has("module")) {
183-
// loadPlugin(item.get("module"), item.get("options"));
184-
// } else {
185-
// loadPlugin(item);
186-
// }
187-
// }
188-
//});
189-
//
190-
//function loadPlugin (name, opts) {
191-
// opts = opts ? opts.toJS() : {};
192-
// if (_.isString(name)) {
193-
// opts.moduleName = name;
194-
// bs.registerPlugin(require(name), opts);
195-
// } else {
196-
// bs.registerPlugin(name.toJS(), opts);
197-
// }
198-
//}
199-
200137
done();
201138
},
202139
/**

lib/logger.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ module.exports.callbacks = {
7575
"config:warn": function (bs, data) {
7676
logger.setOnce("useLevelPrefixes", true).warn(data.msg);
7777
},
78+
/**
79+
* @param {BrowserSync} bs
80+
* @param data
81+
*/
82+
"config:error": function (bs, data) {
83+
logger.setOnce("useLevelPrefixes", true).error.apply(logger, data.msg);
84+
},
7885
/**
7986
* @param {BrowserSync} bs
8087
* @param data

lib/plugins.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
var Immutable = require("immutable");
2+
var qs = require("qs");
3+
4+
function resolvePlugin(item) {
5+
6+
if (typeof item === "string") {
7+
return getFromString(item);
8+
}
9+
10+
if (Immutable.Map.isMap(item)) {
11+
12+
if (item.has("module")) {
13+
14+
var nameOrObj = item.get("module");
15+
var options = item.get("options");
16+
17+
if (options) {
18+
options = options.toJS();
19+
} else {
20+
options = {}
21+
}
22+
23+
if (typeof nameOrObj === "string") {
24+
var plugin = getFromString(nameOrObj);
25+
plugin.options = options;
26+
return plugin;
27+
}
28+
29+
if (Immutable.Map.isMap(nameOrObj)) {
30+
return {
31+
module: nameOrObj.toJS(),
32+
options: options
33+
}
34+
}
35+
}
36+
if (item.has("plugin")) {
37+
// top level plugin
38+
var mod = item.toJS();
39+
return {
40+
module: mod,
41+
options: {}
42+
}
43+
}
44+
}
45+
return {
46+
name: item,
47+
options: {}
48+
}
49+
}
50+
51+
module.exports.resolvePlugin = resolvePlugin;
52+
53+
function requirePlugin (item) {
54+
if (!item.module) {
55+
try {
56+
item.module = require(item.name);
57+
} catch (e) {
58+
if (e.code === "MODULE_NOT_FOUND") {
59+
item.errors = [e];
60+
} else {
61+
throw e;
62+
}
63+
}
64+
}
65+
return item;
66+
}
67+
module.exports.requirePlugin = requirePlugin;
68+
69+
function getFromString(string) {
70+
var split = string.split("?");
71+
if (split.length > 1) {
72+
return {
73+
moduleName: split[0],
74+
name: split[0],
75+
options: qs.parse(split[1])
76+
}
77+
}
78+
return {
79+
moduleName: split[0],
80+
name: split[0],
81+
options: {}
82+
};
83+
}
84+

lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var utils = {
174174
},
175175
/**
176176
* @param {Boolean} kill
177-
* @param {String|Error} errMessage
177+
* @param {String|Error} [errMessage]
178178
* @param {Function} [cb]
179179
*/
180180
fail: function (kill, errMessage, cb) {

test/specs/plugins/user.plugins.inline.enabled.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe("Plugins: Setting the default state (false) if given in options", funct
3030
after(function () {
3131
instance.cleanup();
3232
});
33-
it("Should auto disable a plugin when options given", function (done) {
33+
it("Should auto disable a plugin when options given (1)", function (done) {
3434
assert.equal(instance.getUserPlugins().length, 1);
3535
assert.isFalse(instance.getUserPlugins()[0].active);
3636
done();
@@ -62,7 +62,7 @@ describe("Plugins: Setting the default state (true) if given in options", functi
6262
after(function () {
6363
instance.cleanup();
6464
});
65-
it("Should auto disable a plugin when options given", function (done) {
65+
it("Should auto disable a plugin when options given (2)", function (done) {
6666
assert.equal(instance.getUserPlugins().length, 1);
6767
assert.isTrue(instance.getUserPlugins()[0].active);
6868
done();

test/specs/plugins/user.plugins.inline.options.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var assert = require("chai").assert;
77
describe("Plugins: Retrieving user plugins when given inline with options", function () {
88

99
var instance;
10-
var MODULE_REQUIRE = "bs-snippet-injector";
1110
var PLUGIN_NAME = "Snippet Injector";
1211

1312
before(function (done) {
@@ -43,7 +42,6 @@ describe("Plugins: Retrieving user plugins when given inline with options", func
4342
it("should have access to user provided opts", function (done) {
4443
var plugin = instance.getUserPlugins()[0];
4544
assert.equal(plugin.opts.files, "*.html");
46-
assert.equal(plugin.opts.moduleName, MODULE_REQUIRE);
4745
done();
4846
});
4947
});

0 commit comments

Comments
 (0)