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 { class Plugins {
constructor() { constructor() {
this.plugin = new Plugin(); this.plugin = new Plugin();
this.plugins = [];
} }
createPlugin() { createPlugin(name) {
return this.plugin; let plugin = new Plugin({ name });
this.plugins.push(plugin);
return plugin;
} }
emitAndRunActionsForEvent(name, options, callback) { emitAndRunActionsForEvent(name, options, callback) {
this.runActionsForEvent(name, options, callback);
}
runActionsForEvent(name, options, callback) {
const listeners = this.plugin.getListeners(name); const listeners = this.plugin.getListeners(name);
if (listeners) { if (listeners) {
listeners.forEach(fn => fn(options, callback)); listeners.forEach(fn => fn(options, callback));
} else {
callback(null, options);
} }
} }
@ -20,12 +29,27 @@ class Plugins {
teardown() { teardown() {
this.plugin.listeners = {}; 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 { class Plugin {
constructor() { constructor() {
this.listeners = {}; this.listeners = {};
this.pluginTypes = [];
this.compilers = [];
} }
getListeners(name) { getListeners(name) {
@ -38,6 +62,24 @@ class Plugin {
} }
this.listeners[name].push(action); 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; module.exports = Plugins;