feat(@embark/testing): introduce plugin APIs to register compilers

Embark relies on certain specific plugin properties, e.g. registered compilers,
and retrieves them using `plugins.getPluginProperties('compilers', 'compilers')`.

In order to make this work in the testing environment, we need those same APIs
in the `embark-testing` package as well.

This commit adds necessary APIs to `Plugins` and `Plugin` to make registering and
loading compiler plugins work.
This commit is contained in:
Pascal Precht 2019-10-23 13:54:12 +09:00 committed by Michael Bradley
parent f7cb1ada63
commit f289a6fc6a
1 changed files with 44 additions and 2 deletions

View File

@ -1,16 +1,25 @@
class Plugins {
constructor() {
this.plugin = new Plugin();
this.plugins = [];
}
createPlugin() {
return this.plugin;
createPlugin(name) {
let plugin = new Plugin({ name });
this.plugins.push(plugin);
return plugin;
}
emitAndRunActionsForEvent(name, options, callback) {
this.runActionsForEvent(name, options, callback);
}
runActionsForEvent(name, options, callback) {
const listeners = this.plugin.getListeners(name);
if (listeners) {
listeners.forEach(fn => fn(options, callback));
} else {
callback(null, options);
}
}
@ -20,12 +29,27 @@ class Plugins {
teardown() {
this.plugin.listeners = {};
this.plugins.forEach(plugin => plugin.teardown());
}
getPluginsProperty(pluginType, prop, childProp) {
let plugins = this.plugins.filter(plugin => plugin.has(pluginType));
let properties = plugins.map(plugin => {
if (childProp) {
return plugin[prop][childProp];
}
return plugin[prop];
});
return properties.length > 0 ? properties.reduce((a,b) => { return a.concat(b); }) || [] : [];
}
}
class Plugin {
constructor() {
this.listeners = {};
this.pluginTypes = [];
this.compilers = [];
}
getListeners(name) {
@ -38,6 +62,24 @@ class Plugin {
}
this.listeners[name].push(action);
}
has(pluginType) {
return this.pluginTypes.indexOf(pluginType) >= 0;
}
addPluginType(pluginType) {
this.pluginTypes.push(pluginType);
this.pluginTypes = Array.from(new Set(this.pluginTypes));
}
registerCompiler(extension, cb) {
this.compilers.push({extension: extension, cb: cb});
this.addPluginType('compilers');
}
teardown() {
this.compilers = [];
}
}
module.exports = Plugins;