|
1 | | -var Immutable = require("immutable"); |
2 | | -var qs = require("qs"); |
| 1 | +var Immutable = require("immutable"); |
| 2 | +var Map = Immutable.Map; |
| 3 | +var List = Immutable.List; |
| 4 | +var qs = require("qs"); |
| 5 | +var path = require("path"); |
| 6 | +var fs = require("fs"); |
3 | 7 |
|
| 8 | +var Plugin = Immutable.Record({ |
| 9 | + moduleName: "", |
| 10 | + name: "", |
| 11 | + active: true, |
| 12 | + module: undefined, |
| 13 | + options: Map({}), |
| 14 | + via: "inline", |
| 15 | + dir: process.cwd(), |
| 16 | + init: undefined, |
| 17 | + errors: List([]) |
| 18 | +}); |
| 19 | + |
| 20 | +/** |
| 21 | + * Accept a string/object |
| 22 | + * and resolve it into the plugin format above |
| 23 | + * @param item |
| 24 | + * @returns {*} |
| 25 | + */ |
4 | 26 | function resolvePlugin(item) { |
5 | 27 |
|
| 28 | + /** |
| 29 | + * Handle when string was given, such as plugins: ['bs-html-injector'] |
| 30 | + */ |
6 | 31 | if (typeof item === "string") { |
7 | 32 | return getFromString(item); |
8 | 33 | } |
9 | 34 |
|
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"); |
| 35 | + if (item.has("module")) { |
16 | 36 |
|
17 | | - if (options) { |
18 | | - options = options.toJS(); |
19 | | - } else { |
20 | | - options = {} |
21 | | - } |
| 37 | + var nameOrObj = item.get("module"); |
| 38 | + var options = item.get("options"); |
22 | 39 |
|
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(), |
| 40 | + /** |
| 41 | + * The 'module' key can be a string, this allows |
| 42 | + * inline plugin references, but with options |
| 43 | + * eg: |
| 44 | + * |
| 45 | + * bs.init({ |
| 46 | + * plugins: [ |
| 47 | + * { |
| 48 | + * module: './myjs-file.js' |
| 49 | + * options: { |
| 50 | + * files: "*.html" |
| 51 | + * } |
| 52 | + * } |
| 53 | + * ] |
| 54 | + * }); |
| 55 | + */ |
| 56 | + if (typeof nameOrObj === "string") { |
| 57 | + return getFromString(nameOrObj) |
| 58 | + .mergeDeep({ |
32 | 59 | options: options |
33 | | - } |
34 | | - } |
| 60 | + }); |
35 | 61 | } |
36 | | - if (item.has("plugin")) { |
37 | | - // top level plugin |
38 | | - var mod = item.toJS(); |
39 | | - return { |
40 | | - module: mod, |
41 | | - options: {} |
42 | | - } |
| 62 | + |
| 63 | + /** |
| 64 | + * If the plugin was given completely inline (because it needs options) |
| 65 | + * eg: |
| 66 | + * |
| 67 | + * bs.init({ |
| 68 | + * plugins: [ |
| 69 | + * { |
| 70 | + * module: { |
| 71 | + * plugin: function() { |
| 72 | + * console.log('My plugin code') |
| 73 | + * } |
| 74 | + * }, |
| 75 | + * options: { |
| 76 | + * files: "*.html" |
| 77 | + * } |
| 78 | + * } |
| 79 | + * ] |
| 80 | + * }) |
| 81 | + */ |
| 82 | + if (Immutable.Map.isMap(nameOrObj)) { |
| 83 | + return new Plugin({ |
| 84 | + module: nameOrObj, |
| 85 | + options: options |
| 86 | + }); |
43 | 87 | } |
44 | 88 | } |
45 | | - return { |
46 | | - name: item, |
47 | | - options: {} |
| 89 | + |
| 90 | + /** |
| 91 | + * If a module was given directly. For example, ater calling require. |
| 92 | + * |
| 93 | + * eg: |
| 94 | + * var myplugin = require('./some-js'); |
| 95 | + * bs.init({plugins: [myplugin]}); |
| 96 | + */ |
| 97 | + if (item.has("plugin")) { |
| 98 | + return new Plugin({ |
| 99 | + module: item |
| 100 | + }) |
48 | 101 | } |
| 102 | + |
| 103 | + /** |
| 104 | + * If we reach here, the plugin option was used incorrectly |
| 105 | + */ |
| 106 | + return new Plugin().mergeDeep({errors: [new Error("Plugin was not configured correctly")]}) |
49 | 107 | } |
50 | 108 |
|
51 | 109 | module.exports.resolvePlugin = resolvePlugin; |
52 | 110 |
|
| 111 | +/** |
| 112 | + * Load a plugin from disk |
| 113 | + * @param item |
| 114 | + * @returns {*} |
| 115 | + */ |
53 | 116 | 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]; |
| 117 | + |
| 118 | + /** |
| 119 | + * if the "module" property already exists and |
| 120 | + * is not a string, then we bail and don't bother looking |
| 121 | + * for the file |
| 122 | + */ |
| 123 | + if (item.get("module") && typeof item.get("module") !== "string") { |
| 124 | + return item; |
| 125 | + } |
| 126 | + |
| 127 | + try { |
| 128 | + /** |
| 129 | + * Try a raw node require() call - this will be how |
| 130 | + * regular "npm installed" plugins wil work |
| 131 | + */ |
| 132 | + return item.set("module", require(item.get("name"))); |
| 133 | + } catch (e) { |
| 134 | + /** |
| 135 | + * If require threw an MODULE_NOT_FOUND error, try again |
| 136 | + * by resolving from cwd. This is needed since cli |
| 137 | + * users will not add ./ to the front of a path (which |
| 138 | + * node requires to resolve from cwd) |
| 139 | + */ |
| 140 | + if (e.code === "MODULE_NOT_FOUND") { |
| 141 | + var maybe = path.resolve(process.cwd(), item.get("name")); |
| 142 | + if (fs.existsSync(maybe)) { |
| 143 | + return item.set("module", require(maybe)); |
60 | 144 | } else { |
61 | | - throw e; |
| 145 | + /** |
| 146 | + * Finally return a plugin that contains the error |
| 147 | + * this will be picked up later and discarded |
| 148 | + */ |
| 149 | + return item.update("errors", function (errors) { |
| 150 | + return errors.concat(e); |
| 151 | + }); |
62 | 152 | } |
63 | 153 | } |
| 154 | + throw e; |
64 | 155 | } |
65 | | - return item; |
66 | 156 | } |
67 | 157 | module.exports.requirePlugin = requirePlugin; |
68 | 158 |
|
69 | 159 | function getFromString(string) { |
| 160 | + |
| 161 | + /** |
| 162 | + * We allow query strings for plugins, so always split on ? |
| 163 | + */ |
70 | 164 | var split = string.split("?"); |
| 165 | + |
| 166 | + var outGoing = new Plugin({ |
| 167 | + moduleName: split[0], |
| 168 | + name: split[0] |
| 169 | + }); |
| 170 | + |
71 | 171 | if (split.length > 1) { |
72 | | - return { |
73 | | - moduleName: split[0], |
74 | | - name: split[0], |
75 | | - options: qs.parse(split[1]) |
76 | | - } |
| 172 | + return outGoing.update("options", function (opts) { |
| 173 | + return opts.mergeDeep(qs.parse(split[1])); |
| 174 | + }); |
77 | 175 | } |
78 | | - return { |
79 | | - moduleName: split[0], |
80 | | - name: split[0], |
81 | | - options: {} |
82 | | - }; |
| 176 | + |
| 177 | + return outGoing; |
83 | 178 | } |
84 | 179 |
|
0 commit comments