-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.js
More file actions
35 lines (27 loc) · 978 Bytes
/
plugin.js
File metadata and controls
35 lines (27 loc) · 978 Bytes
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
wrap(() => {
const nameRegex = /^[a-z0-9 ]+$/i;
const registry = new Map();
const modules = [];
function Plugin(name = '') {
name = name.trim();
if (name === '') throw new Error('Plugin must have a name');
if (name.length > 20) throw new Error(`Plugin name too long (${name.length}/20)`);
if (!nameRegex.test(name)) throw new Error('Name contains illegal characters');
if (registry.has(name)) throw new Error('Name already registered');
const methods = {
name,
};
modules.forEach(({ name: prop, mod }) => {
// eslint-disable-next-line no-prototype-builtins
if (methods.hasOwnProperty(prop)) return console.error(`Skipping "${prop}": Already exists`);
methods[prop] = mod(methods);
});
const plugin = Object.freeze(methods);
registry.set(name, plugin);
return plugin;
}
api.register('plugin', Plugin);
script.registerModule = (name, mod) => {
modules.push({ name, mod });
};
});