-
Notifications
You must be signed in to change notification settings - Fork 0
/
pluginUtil.js
120 lines (98 loc) · 2.77 KB
/
pluginUtil.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
var _ = require('lodash');
var async = require('async');
var Emitter = require('./emitter');
var util = require(__dirname + '/util');
var log = require(util.dirs().core + 'log');
var config = util.getConfig();
var pluginDir = util.dirs().plugins;
var gekkoMode = util.gekkoMode();
var inherits = require('util').inherits;
var pluginHelper = {
// Checks whether we can load a module
// @param Object plugin
// plugin config object
// @return String
// error message if we can't
// use the module.
cannotLoad: function(plugin) {
// verify plugin dependencies are installed
if(_.has(plugin, 'dependencies'))
var error = false;
_.each(plugin.dependencies, function(dep) {
try {
var a = require(dep.module);
}
catch(e) {
log.error('ERROR LOADING DEPENDENCY', dep.module);
if(!e.message) {
log.error(e);
util.die();
}
if(!e.message.startsWith('Cannot find module'))
return util.die(e);
error = [
'The plugin',
plugin.slug,
'expects the module',
dep.module,
'to be installed.',
'However it is not, install',
'it by running: \n\n',
'\tnpm install',
dep.module + '@' + dep.version
].join(' ');
}
});
return error;
},
// loads a plugin
//
// @param Object plugin
// plugin config object
// @param Function next
// callback
load: function(plugin, next) {
plugin.config = config[plugin.slug];
if(!plugin.config || !plugin.config.enabled)
return next();
if(!_.contains(plugin.modes, gekkoMode)) {
log.warn(
'The plugin',
plugin.name,
'does not support the mode',
gekkoMode + '.',
'It has been disabled.'
)
return next();
}
log.info('Setting up:');
log.info('\t', plugin.name);
log.info('\t', plugin.description);
var cannotLoad = pluginHelper.cannotLoad(plugin);
if(cannotLoad)
return next(cannotLoad);
if(plugin.path)
var Constructor = require(pluginDir + plugin.path(config));
else
var Constructor = require(pluginDir + plugin.slug);
if(plugin.async) {
inherits(Constructor, Emitter);
var instance = new Constructor(util.defer(function(err) {
next(err, instance);
}), plugin);
Emitter.call(instance);
instance.meta = plugin;
} else {
inherits(Constructor, Emitter);
var instance = new Constructor(plugin);
Emitter.call(instance);
instance.meta = plugin;
_.defer(function() {
next(null, instance);
});
}
if(!plugin.silent)
log.info('\n');
}
}
module.exports = pluginHelper;