-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
270 lines (265 loc) · 9.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import {is_object_literal, is_object, merge} from 'mixme';
import toposort from 'toposort';
import error from './error.js';
import {array_flatten} from './utils.js';
const normalize_hook = function(name, hook) {
if (!Array.isArray(hook)) {
hook = [hook];
}
return hook.map(function(hook) {
if (typeof hook === 'function') {
hook = {
handler: hook
};
} else if (!is_object(hook)) {
throw error('PLUGINS_HOOK_INVALID_HANDLER', ['no hook handler function could be found,', 'a hook must be defined as a function', 'or as an object with an handler property,', `got ${JSON.stringify(hook)} instead.`]);
}
hook.name = name;
if (typeof hook.after === 'string') {
hook.after = [hook.after];
}
if (typeof hook.before === 'string') {
hook.before = [hook.before];
}
return hook;
});
};
const errors = {
PLUGINS_HOOK_AFTER_INVALID: function({name, plugin, after}) {
throw error('PLUGINS_HOOK_AFTER_INVALID', [
`the hook ${JSON.stringify(name)}`,
plugin ? `in plugin ${JSON.stringify(plugin)}` : void 0,
'references an after dependency',
`in plugin ${JSON.stringify(after)} which does not exists.`
]);
},
PLUGINS_HOOK_BEFORE_INVALID: function({name, plugin, before}) {
throw error('PLUGINS_HOOK_BEFORE_INVALID', [
`the hook ${JSON.stringify(name)}`,
plugin ? `in plugin ${JSON.stringify(plugin)}` : void 0,
'references a before dependency',
`in plugin ${JSON.stringify(before)} which does not exists.`
]);
},
REQUIRED_PLUGIN: function({plugin, require}){
throw error('REQUIRED_PLUGIN', [
`the plugin ${JSON.stringify(plugin)}`,
'requires a plugin',
`named ${JSON.stringify(require)} which is not unregistered.`
]);
},
PLUGINS_REGISTER_INVALID_REQUIRE: function({name, require}){
throw error('PLUGINS_REGISTER_INVALID_REQUIRE', [
'the `require` property',
name ? `in plugin ${JSON.stringify(name)}`: void 0,
'must be a string or an array,',
`got ${JSON.stringify(require)}.`
]);
}
};
const plugandplay = function({args, chain, parent, plugins = []} = {}) {
// Internal plugin store
const store = [];
// Public API definition
const registry = {
// Register new plugins
register: function(plugin) {
if (typeof plugin === 'function') {
plugin = plugin.apply(null, args);
}
if (!is_object_literal(plugin)) {
throw error('PLUGINS_REGISTER_INVALID_ARGUMENT', ['a plugin must be an object literal or a function returning an object literal', 'with keys such as `name`, `required` and `hooks`,', `got ${JSON.stringify(plugin)} instead.`]);
}
if (plugin.hooks == null) {
plugin.hooks = {};
}
for (let name in plugin.hooks) {
plugin.hooks[name] = normalize_hook(name, plugin.hooks[name]);
}
if (plugin.require == null){
plugin.require = [];
}else if (typeof plugin.require === 'string'){
plugin.require = [plugin.require];
}
if(!Array.isArray(plugin.require)){
throw errors.PLUGINS_REGISTER_INVALID_REQUIRE({name: plugin.name, require: plugin.require})
}
store.push(plugin);
return chain || this;
},
registered: function(name){
for(const plugin of store){
if (plugin.name === name){
return true;
}
}
if(parent != null && parent.registered(name)){
return true;
}
return false;
},
get: function({name, hooks = [], sort = true}) {
hooks = [
...normalize_hook(name, hooks),
...array_flatten(
store
.map(function(plugin){
// Only filter plugins with the requested hook
if(!plugin.hooks[name]) return;
// Validate plugin requirements
for(const require of plugin.require){
if(!registry.registered(require)){
throw errors.REQUIRED_PLUGIN({
plugin: plugin.name,
require: require
});
}
}
return plugin.hooks[name].map(function(hook){
return merge({
plugin: plugin.name,
require: plugin.require
}, hook);
});
})
.filter(function(hook){return hook !== undefined;})
),
...(parent ? parent.get({
name: name,
sort: false
}) : [])
];
if (!sort) {
return hooks;
}
// Topological sort
const index = {};
for(const hook of hooks){
index[hook.plugin] = hook;
}
const edges_after = hooks
.map(function(hook){
if(!hook.after) return;
return hook.after
.map(function(after){
// This check assume the plugin has the same hooks which is not always the case
if(!index[after]){
if(registry.registered(after)){
throw errors.PLUGINS_HOOK_AFTER_INVALID({
name: name,
plugin: hook.plugin,
after: after
});
}else{
return undefined;
}
}
return [index[after], hook];
})
.filter(function(hook){ return hook !== undefined});
})
.filter(function(hook){return hook !== undefined;});
const edges_before = hooks
.map(function(hook){
if(!hook.before) return;
return hook.before.map(function(before){
if(!index[before]){
if(registry.registered(before)){
throw errors.PLUGINS_HOOK_BEFORE_INVALID({
name: name,
plugin: hook.plugin,
before: before
});
}else{
return undefined;
}
}
return [hook, index[before]];
})
.filter(function(hook){ return hook !== undefined});
})
.filter(function(hook){return hook !== undefined;});
const edges = array_flatten([...edges_after, ...edges_before], 0);
return toposort.array(hooks, edges);
},
// Call a hook against each registered plugin matching the hook name
call: async function({args = [], handler, hooks = [], name}) {
if (arguments.length !== 1) {
throw error('PLUGINS_INVALID_ARGUMENTS_NUMBER', ['function `call` expect 1 object argument,', `got ${arguments.length} arguments.`]);
} else if (!is_object_literal(arguments[0])) {
throw error('PLUGINS_INVALID_ARGUMENT_PROPERTIES', ['function `call` expect argument to be a literal object', 'with the properties `name`, `args`, `hooks` and `handler`,', `got ${JSON.stringify(arguments[0])} arguments.`]);
} else if (typeof name !== 'string') {
throw error('PLUGINS_INVALID_ARGUMENT_NAME', ['function `call` requires a property `name` in its first argument,', `got ${JSON.stringify(arguments[0])} argument.`]);
}
// Retrieve the name hooks
hooks = this.get({
hooks: hooks,
name: name
});
// Call the hooks
for(const hook of hooks){
switch (hook.handler.length) {
case 0:
case 1:
await hook.handler.call(this, args);
break;
case 2:
handler = (await hook.handler.call(this, args, handler));
if (handler === null) {
return null;
}
break;
default:
throw error('PLUGINS_INVALID_HOOK_HANDLER', ['hook handlers must have 0 to 2 arguments', `got ${hook.handler.length}`]);
}
}
if (handler) {
// Call the final handler
return handler.call(this, args);
}
},
// Call a hook against each registered plugin matching the hook name
call_sync: function({args = [], handler, hooks = [], name}) {
if (arguments.length !== 1) {
throw error('PLUGINS_INVALID_ARGUMENTS_NUMBER', ['function `call` expect 1 object argument,', `got ${arguments.length} arguments.`]);
} else if (!is_object_literal(arguments[0])) {
throw error('PLUGINS_INVALID_ARGUMENT_PROPERTIES', ['function `call` expect argument to be a literal object', 'with the properties `name`, `args`, `hooks` and `handler`,', `got ${JSON.stringify(arguments[0])} arguments.`]);
} else if (typeof name !== 'string') {
throw error('PLUGINS_INVALID_ARGUMENT_NAME', ['function `call` requires a property `name` in its first argument,', `got ${JSON.stringify(arguments[0])} argument.`]);
}
// Retrieve the name hooks
hooks = this.get({
hooks: hooks,
name: name
});
// Call the hooks
for(const hook of hooks) {
switch (hook.handler.length) {
case 0:
case 1:
hook.handler.call(this, args);
break;
case 2:
handler = hook.handler.call(this, args, handler);
if (handler === null) {
return null;
}
break;
default:
throw error('PLUGINS_INVALID_HOOK_HANDLER', ['hook handlers must have 0 to 2 arguments', `got ${hook.handler.length}`]);
}
}
if (handler) {
// Call the final handler
return handler.call(this, args);
}
}
};
// Register initial plugins
for(const plugin of plugins){
registry.register(plugin);
}
// return the object
return registry;
};
export {plugandplay};