embark-area-51/lib/core/plugins.js

78 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-03-31 11:34:43 +00: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;
2017-03-30 11:12:39 +00:00
};
2017-03-31 11:34:43 +00:00
Plugins.prototype.loadPlugins = function() {
var pluginConfig;
for (var pluginName in this.pluginList) {
pluginConfig = this.pluginList[pluginName];
this.loadPlugin(pluginName, pluginConfig);
}
2017-03-31 11:34:43 +00:00
};
2017-03-31 11:34:43 +00:00
Plugins.prototype.listPlugins = function() {
var list = [];
for (var className in this.pluginList) {
list.push(className);
2017-03-30 13:16:46 +00:00
}
2017-03-31 11:34:43 +00:00
return list;
};
2017-03-30 13:16:46 +00:00
// for services that act as a plugin but have core functionality
Plugins.prototype.createPlugin = function(pluginName, pluginConfig) {
let plugin = {};
2017-12-30 22:07:13 +00:00
let pluginPath = false;
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});
this.plugins.push(pluginWrapper);
return pluginWrapper;
};
2017-12-16 20:39:30 +00:00
Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) {
var pluginPath = utils.joinPath('../modules/', pluginName, 'index.js');
var plugin = require(pluginPath);
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});
pluginWrapper.loadInternalPlugin();
this.plugins.push(pluginWrapper);
};
2017-03-31 11:34:43 +00:00
Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) {
var pluginPath = utils.joinPath(utils.pwd(), 'node_modules', pluginName);
2017-03-31 11:34:43 +00:00
var plugin = require(pluginPath);
2017-12-16 20:39:30 +00: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});
2017-03-31 11:34:43 +00:00
pluginWrapper.loadPlugin();
this.plugins.push(pluginWrapper);
};
2017-03-30 13:16:46 +00:00
2017-03-31 11:34:43 +00:00
Plugins.prototype.getPluginsFor = function(pluginType) {
return this.plugins.filter(function(plugin) {
return plugin.has(pluginType);
});
};
Plugins.prototype.getPluginsProperty = function(pluginType, property) {
let matchingPlugins = this.plugins.filter(function(plugin) {
return plugin.has(pluginType);
});
2017-12-29 13:26:31 +00:00
let matchingProperties = matchingPlugins.map((plugin) => {
return plugin[property];
});
2017-12-29 13:26:31 +00:00
//return flattened list
if (matchingProperties.length === 0) return [];
2017-12-29 13:46:01 +00:00
return matchingProperties.reduce((a,b) => { return a.concat(b); });
};
2017-03-31 11:34:43 +00:00
module.exports = Plugins;