-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.92 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
module.exports = class Registration {
constructor() {
this.available = {};
this.loaded = {};
this.closureCount = 0;
}
register(providerNames) {
if (typeof providerNames === 'string') {
let [name, provider] = arguments
providerNames = {};
providerNames[name] = provider;
}
for (let key in providerNames) {
let provider = providerNames[key];
// Check if the provider is a function. if it is, accept it, and make it
// conform to the register style object
if (typeof provider === 'function') {
this.available['closure_' + this.closureCount ++] = {register:provider}
delete providerNames[key]
} else if (typeof provider === 'object' && provider.hasOwnProperty('register')) {
// pass the object as
this.available[key] = provider
} else if(typeof provider === 'string'){
try {
// Try to require the provider, if we cannot then just log the error.
this.available[key] = require(provider);
} catch (err) {
console.log('[!] Failed to load provider [' + provider + ']');
}
}
}
// Loop through the providers
for (let provider in this.available) {
if (typeof this.available[provider].register !== 'function') {
// If register isn't a function, don't try to run it...
break;
}
// Run register
let result = this.available[provider].register();
// If it returns false don't load it.
if (result == false) {
break;
}
this.loaded[provider] = result;
}
}
}