-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
73 lines (54 loc) · 1.6 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
'use strict';
const debug = require('debug')('ember-cli:registry');
class Registry {
constructor() {
this.registry = {};
}
extensionsForType(type) {
let registered = this.registeredForType(type);
let extensions = registered
.reduce((memo, plugin) => memo.concat(plugin.ext), [type])
.filter(Boolean)
.reduce((memo, ext) => {
if (memo.indexOf(ext) === -1) {
memo.push(ext);
}
return memo;
}, []);
debug('extensions for type %s: %s', type, extensions);
return extensions;
}
load(type) {
let plugins = this.registeredForType(type);
debug('loading %s: available plugins %s;', type, plugins.map(p => p.name));
return plugins;
}
registeredForType(type) {
return this.registry[type] = this.registry[type] || [];
}
add(type, plugin) {
let registered = this.registeredForType(type);
debug('add type: %s, name: %s, extension:%s', type, plugin.name, plugin.ext);
registered.push(plugin);
}
remove(type, pluginOrName) {
let registered = this.registeredForType(type);
let name, registeredIndex;
if (typeof pluginOrName === 'object') {
name = pluginOrName.name;
registeredIndex = registered.indexOf(pluginOrName);
} else {
name = pluginOrName;
for (let i = 0, l = registered.length; i < l; i++) {
if (registered[i].name === name) {
registeredIndex = i;
}
}
}
debug('remove type: %s, name: %s', type, name);
if (registeredIndex > -1) {
registered.splice(registeredIndex, 1);
}
}
}
module.exports = Registry;