2017-03-31 07:34:43 -04:00
|
|
|
var Plugin = require('./plugin.js');
|
|
|
|
var utils = require('../utils/utils.js');
|
|
|
|
|
|
|
|
var Plugins = function(options) {
|
|
|
|
this.pluginList = options.plugins || [];
|
|
|
|
this.interceptLogs = options.interceptLogs;
|
|
|
|
this.plugins = [];
|
|
|
|
// TODO: need backup 'NullLogger'
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.events = options.events;
|
|
|
|
this.config = options.config;
|
2018-04-25 10:34:17 -04:00
|
|
|
this.context = options.context;
|
2018-05-18 22:40:47 -04:00
|
|
|
this.env = options.env;
|
2017-03-30 20:12:39 +09:00
|
|
|
};
|
|
|
|
|
2017-03-31 07:34:43 -04:00
|
|
|
Plugins.prototype.loadPlugins = function() {
|
|
|
|
var pluginConfig;
|
|
|
|
for (var pluginName in this.pluginList) {
|
|
|
|
pluginConfig = this.pluginList[pluginName];
|
|
|
|
this.loadPlugin(pluginName, pluginConfig);
|
2017-03-30 00:37:30 +09:00
|
|
|
}
|
2017-03-31 07:34:43 -04:00
|
|
|
};
|
2016-12-06 21:33:31 -05:00
|
|
|
|
2017-03-31 07:34:43 -04:00
|
|
|
Plugins.prototype.listPlugins = function() {
|
2018-04-24 15:53:19 -04:00
|
|
|
const list = [];
|
|
|
|
this.plugins.forEach(plugin => {
|
|
|
|
if (plugin.loaded) {
|
|
|
|
list.push(plugin.name);
|
|
|
|
}
|
|
|
|
});
|
2017-03-31 07:34:43 -04:00
|
|
|
return list;
|
|
|
|
};
|
2017-03-30 22:16:46 +09:00
|
|
|
|
2017-12-30 15:52:51 -05:00
|
|
|
// for services that act as a plugin but have core functionality
|
|
|
|
Plugins.prototype.createPlugin = function(pluginName, pluginConfig) {
|
|
|
|
let plugin = {};
|
2017-12-30 17:07:13 -05:00
|
|
|
let pluginPath = false;
|
2018-04-25 10:34:17 -04:00
|
|
|
var pluginWrapper = new Plugin({
|
|
|
|
name: pluginName,
|
|
|
|
pluginModule: plugin,
|
|
|
|
pluginConfig: pluginConfig,
|
|
|
|
logger: this.logger,
|
|
|
|
pluginPath: pluginPath,
|
|
|
|
interceptLogs: this.interceptLogs,
|
|
|
|
events: this.events,
|
|
|
|
config: this.config,
|
|
|
|
isInternal: true,
|
|
|
|
context: this.context
|
|
|
|
});
|
2017-12-30 15:52:51 -05:00
|
|
|
this.plugins.push(pluginWrapper);
|
|
|
|
return pluginWrapper;
|
|
|
|
};
|
|
|
|
|
2017-12-16 15:39:30 -05:00
|
|
|
Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) {
|
|
|
|
var pluginPath = utils.joinPath('../modules/', pluginName, 'index.js');
|
|
|
|
var plugin = require(pluginPath);
|
|
|
|
|
2018-04-25 10:34:17 -04:00
|
|
|
var pluginWrapper = new Plugin({
|
|
|
|
name: pluginName,
|
|
|
|
pluginModule: plugin,
|
|
|
|
pluginConfig: pluginConfig,
|
|
|
|
logger: this.logger,
|
|
|
|
pluginPath: pluginPath,
|
|
|
|
interceptLogs: this.interceptLogs,
|
|
|
|
events: this.events,
|
|
|
|
config: this.config,
|
|
|
|
isInternal: true,
|
2018-05-18 22:40:47 -04:00
|
|
|
context: this.context,
|
|
|
|
env: this.env
|
2018-04-25 10:34:17 -04:00
|
|
|
});
|
2017-12-16 15:39:30 -05:00
|
|
|
pluginWrapper.loadInternalPlugin();
|
|
|
|
this.plugins.push(pluginWrapper);
|
|
|
|
};
|
|
|
|
|
2017-03-31 07:34:43 -04:00
|
|
|
Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) {
|
2018-03-26 15:08:07 -04:00
|
|
|
var pluginPath = utils.joinPath(utils.pwd(), 'node_modules', pluginName);
|
2017-03-31 07:34:43 -04:00
|
|
|
var plugin = require(pluginPath);
|
2017-03-30 00:37:30 +09:00
|
|
|
|
2018-04-25 10:34:17 -04:00
|
|
|
var pluginWrapper = new Plugin({
|
|
|
|
name: pluginName,
|
|
|
|
pluginModule: plugin,
|
|
|
|
pluginConfig: pluginConfig,
|
|
|
|
logger: this.logger,
|
|
|
|
pluginPath: pluginPath,
|
|
|
|
interceptLogs: this.interceptLogs,
|
|
|
|
events: this.events,
|
|
|
|
config: this.config,
|
|
|
|
isInternal: false,
|
|
|
|
context: this.context
|
|
|
|
});
|
2017-03-31 07:34:43 -04:00
|
|
|
pluginWrapper.loadPlugin();
|
|
|
|
this.plugins.push(pluginWrapper);
|
|
|
|
};
|
2017-03-30 22:16:46 +09:00
|
|
|
|
2017-03-31 07:34:43 -04:00
|
|
|
Plugins.prototype.getPluginsFor = function(pluginType) {
|
|
|
|
return this.plugins.filter(function(plugin) {
|
|
|
|
return plugin.has(pluginType);
|
|
|
|
});
|
|
|
|
};
|
2017-03-30 00:37:30 +09:00
|
|
|
|
2017-12-29 08:08:04 -05:00
|
|
|
Plugins.prototype.getPluginsProperty = function(pluginType, property) {
|
|
|
|
let matchingPlugins = this.plugins.filter(function(plugin) {
|
|
|
|
return plugin.has(pluginType);
|
|
|
|
});
|
|
|
|
|
2017-12-29 08:26:31 -05:00
|
|
|
let matchingProperties = matchingPlugins.map((plugin) => {
|
2017-12-29 08:08:04 -05:00
|
|
|
return plugin[property];
|
|
|
|
});
|
2017-12-29 08:26:31 -05:00
|
|
|
|
|
|
|
//return flattened list
|
|
|
|
if (matchingProperties.length === 0) return [];
|
2017-12-29 08:46:01 -05:00
|
|
|
return matchingProperties.reduce((a,b) => { return a.concat(b); });
|
2017-12-29 08:08:04 -05:00
|
|
|
};
|
|
|
|
|
2017-03-31 07:34:43 -04:00
|
|
|
module.exports = Plugins;
|