2017-03-29 15:37:30 +00:00
|
|
|
const _ = require('underscore');
|
|
|
|
const EventEmitter = require('events').EventEmitter;
|
2016-12-07 02:33:31 +00:00
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
let Plugins = function (options) {
|
|
|
|
//TODO: put an observer on this.plugins and call loadPlugin when a new item is added
|
|
|
|
this.config = {};
|
|
|
|
for (let opt in options) {
|
|
|
|
if (options.hasOwnProperty(opt)) {
|
|
|
|
this.config[opt] = options[opt];
|
|
|
|
}
|
2016-12-07 02:33:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
let requiredOptions = ['interceptLogs', 'plugins', 'logger'];
|
|
|
|
for (let i = 0; requiredOptions.length > i; i++) {
|
|
|
|
if (!(_.contains(Object.keys(this.config), requiredOptions[i]))) {
|
|
|
|
console.log('Warning: missing required plugin configuration key: ' + requiredOptions[i]);
|
|
|
|
}
|
2017-01-15 19:30:41 +00:00
|
|
|
}
|
2017-03-29 15:37:30 +00:00
|
|
|
this.on('load', () => {
|
|
|
|
this.load();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on('get', (pluginType, cb) => {
|
|
|
|
let pluginTypes = getPluginsFor(pluginType, this.config.plugins);
|
|
|
|
return cb(pluginTypes);
|
|
|
|
});
|
|
|
|
|
2017-01-15 19:30:41 +00:00
|
|
|
};
|
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
Plugins.prototype.load = Plugins.prototype.loadPlugins = function () {
|
|
|
|
let pluginConfig;
|
|
|
|
for (let i = 0; this.config.plugins.length > i; i++) {
|
|
|
|
pluginConfig = this.config.plugins[i].config;
|
|
|
|
let Plugin = require('./plugin');
|
|
|
|
let plugin = new Plugin(pluginConfig);
|
|
|
|
plugin.run();
|
|
|
|
}
|
|
|
|
};
|
2016-12-07 02:33:31 +00:00
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
Plugins.prototype.listPlugins = function () {
|
|
|
|
return this.config.plugins.join(', ');
|
2016-12-07 02:33:31 +00:00
|
|
|
};
|
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
let getPluginsFor = function (pluginType, plugins) {
|
|
|
|
return _.filter(plugins, pluginType);
|
2016-12-07 02:33:31 +00:00
|
|
|
};
|
|
|
|
|
2017-03-29 15:37:30 +00:00
|
|
|
Plugins.prototype.getPluginsFor = getPluginsFor;
|
|
|
|
|
|
|
|
Plugins.prototype = Object.create(EventEmitter.prototype);
|
|
|
|
|
|
|
|
module.exports = Plugins;
|