embark/lib/core/plugins.js

164 lines
4.3 KiB
JavaScript
Raw Normal View History

const async = require('async');
2017-03-31 11:34:43 +00:00
var Plugin = require('./plugin.js');
var fs = require('../core/fs.js');
2017-03-31 11:34:43 +00:00
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;
this.context = options.context;
2018-05-19 02:40:47 +00:00
this.env = options.env;
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() {
const list = [];
this.plugins.forEach(plugin => {
if (plugin.loaded) {
list.push(plugin.name);
}
});
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,
2018-03-13 10:23:12 +00:00
//plugins: this,
isInternal: true,
context: this.context
});
this.plugins.push(pluginWrapper);
return pluginWrapper;
};
2017-12-16 20:39:30 +00:00
Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) {
2018-06-18 15:27:29 +00:00
var pluginPath = fs.embarkPath('lib/modules/' + pluginName);
2017-12-16 20:39:30 +00:00
var plugin = require(pluginPath);
var pluginWrapper = new Plugin({
name: pluginName,
pluginModule: plugin,
2018-05-30 16:26:49 +00:00
pluginConfig: pluginConfig || {},
logger: this.logger,
2018-06-18 15:27:29 +00:00
pluginPath: pluginPath,
interceptLogs: this.interceptLogs,
events: this.events,
config: this.config,
2018-03-13 10:23:12 +00:00
//plugins: this.plugins,
isInternal: true,
2018-05-19 02:40:47 +00:00
context: this.context,
env: this.env
});
2017-12-16 20:39:30 +00:00
pluginWrapper.loadInternalPlugin();
this.plugins.push(pluginWrapper);
};
2017-03-31 11:34:43 +00:00
Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) {
2018-08-17 17:41:39 +00:00
var pluginPath = fs.dappPath('node_modules', pluginName);
2017-03-31 11:34:43 +00:00
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: false,
context: this.context
});
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, sub_property) {
let matchingPlugins = this.plugins.filter(function(plugin) {
return plugin.has(pluginType);
});
// Sort internal plugins first
matchingPlugins.sort((a, b) => {
if (a.isInternal) {
return -1;
}
if (b.isInternal) {
return 1;
}
return 0;
});
2017-12-29 13:26:31 +00:00
let matchingProperties = matchingPlugins.map((plugin) => {
if (sub_property) {
return plugin[property][sub_property];
}
return plugin[property];
});
2017-12-29 13:26:31 +00:00
// Remove empty properties
matchingProperties = matchingProperties.filter((property) => property);
2017-12-29 13:26:31 +00:00
//return flattened list
if (matchingProperties.length === 0) return [];
return matchingProperties.reduce((a,b) => { return a.concat(b); }) || [];
};
2018-05-28 23:40:55 +00:00
Plugins.prototype.runActionsForEvent = function(eventName, args, cb) {
2018-05-29 13:58:19 +00:00
if (typeof (args) === 'function') {
2018-05-28 23:40:55 +00:00
cb = args;
}
let actionPlugins = this.getPluginsProperty('eventActions', 'eventActions', eventName);
2018-05-28 23:40:55 +00:00
if (actionPlugins.length === 0) {
return cb(args);
2018-05-28 23:40:55 +00:00
}
async.reduce(actionPlugins, args, function(current_args, plugin, nextEach) {
2018-05-29 13:58:19 +00:00
if (typeof (args) === 'function') {
plugin.call(plugin, (params) => {
nextEach(null, (params || current_args));
});
2018-05-28 23:40:55 +00:00
} else {
plugin.call(plugin, args, (params) => {
nextEach(null, (params || current_args));
});
2018-05-28 23:40:55 +00:00
}
}, cb);
};
2018-05-30 12:00:31 +00:00
Plugins.prototype.emitAndRunActionsForEvent = function(eventName, args, cb) {
this.events.emit(eventName);
return this.runActionsForEvent(eventName, args, cb);
};
2017-03-31 11:34:43 +00:00
module.exports = Plugins;