mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Move all functions and classes currently available through lib/name and libName to be under a single flat namespace in lib. This make reorganising the lib namespace much simpler as the location of symbols can move without breaking the consumers of lib. The multi-entrypoint design was already problematic for the web plugin system which couldn't effectively use it due to how webpack's shared module system is awkward to set up with multiple entrypoints per package.
120 lines
3.5 KiB
JavaScript
120 lines
3.5 KiB
JavaScript
/* eslint-disable node/global-require */
|
|
/**
|
|
* Helpers for loading plugins in Node.js
|
|
* @module lib/plugin_loader
|
|
*/
|
|
"use strict";
|
|
|
|
const libErrors = require("./errors");
|
|
const libPlugin = require("./plugin");
|
|
const { logger } = require("./logging");
|
|
const path = require("path");
|
|
|
|
|
|
/**
|
|
* Load plugin information
|
|
*
|
|
* Loads plugin info modules for the paths to the given plugins. Once
|
|
* loaded the info modules will not be reloaded should this function be
|
|
* called again.
|
|
*
|
|
* @param {Map<string, string>} pluginList -
|
|
* Mapping of plugin name to require path for the plugins to load.
|
|
* @returns {Promise<Array<Object>>} Array of plugin info modules.
|
|
* @static
|
|
*/
|
|
async function loadPluginInfos(pluginList) {
|
|
let plugins = [];
|
|
for (let [pluginName, pluginPath] of pluginList) {
|
|
let pluginInfo;
|
|
let pluginPackage;
|
|
|
|
try {
|
|
pluginInfo = require(path.posix.join(pluginPath, "info"));
|
|
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
|
|
|
|
} catch (err) {
|
|
throw new libErrors.PluginError(pluginName, err);
|
|
}
|
|
|
|
if (pluginInfo.name !== pluginName) {
|
|
throw new libErrors.EnvironmentError(
|
|
`Expected plugin at ${pluginPath} to be named ${pluginName} but got ${pluginInfo.name}`
|
|
);
|
|
}
|
|
|
|
// migrate: ignore incompatible old plugins
|
|
if (pluginInfo.messages && !(pluginInfo.messages instanceof Array)) {
|
|
logger.warn(`Ignoring incompatible pre alpha.14 plugin ${pluginName}`);
|
|
continue;
|
|
}
|
|
|
|
pluginInfo.requirePath = pluginPath;
|
|
pluginInfo.version = pluginPackage.version;
|
|
plugins.push(pluginInfo);
|
|
}
|
|
return plugins;
|
|
}
|
|
|
|
function loadPluginClass(entrypointName, className, pluginClass, pluginInfo) {
|
|
let resolvedPath = path.posix.join(pluginInfo.requirePath, pluginInfo[entrypointName]);
|
|
let entrypoint = require(resolvedPath);
|
|
if (!entrypoint[className]) {
|
|
throw new libErrors.PluginError(pluginInfo.name,
|
|
new Error(`Expected ${resolvedPath} to export a class named ${className}`)
|
|
);
|
|
}
|
|
if (!(entrypoint[className].prototype instanceof pluginClass)) {
|
|
throw new libErrors.PluginError(pluginInfo.name,
|
|
new Error(`Expected ${className} exported from ${resolvedPath} to be a subclass of ${pluginClass.name}`)
|
|
);
|
|
}
|
|
return entrypoint[className];
|
|
}
|
|
|
|
/**
|
|
* Load controller plugin class of a plugin
|
|
*
|
|
* @param {Object} pluginInfo -
|
|
* Plugin info object returned from {@link
|
|
* module:lib.loadPluginInfos} to load class from.
|
|
* @returns {Promise<function>} plugin class
|
|
* @static
|
|
*/
|
|
async function loadControllerPluginClass(pluginInfo) {
|
|
return loadPluginClass("controllerEntrypoint", "ControllerPlugin", libPlugin.BaseControllerPlugin, pluginInfo);
|
|
}
|
|
|
|
/**
|
|
* Load instance plugin class of a plugin
|
|
*
|
|
* @param {Object} pluginInfo -
|
|
* Plugin info object returned from {@link
|
|
* module:lib.loadPluginInfos} to load class from.
|
|
* @returns {Promise<function>} plugin class
|
|
* @static
|
|
*/
|
|
async function loadInstancePluginClass(pluginInfo) {
|
|
return loadPluginClass("instanceEntrypoint", "InstancePlugin", libPlugin.BaseInstancePlugin, pluginInfo);
|
|
}
|
|
|
|
/**
|
|
* Load control plugin class of a plugin
|
|
*
|
|
* @param {Object} pluginInfo -
|
|
* Plugin info object returned from {@link
|
|
* module:lib.loadPluginInfos} to load class from.
|
|
* @returns {Promise<function>} plugin class
|
|
* @static
|
|
*/
|
|
async function loadControlPluginClass(pluginInfo) {
|
|
return loadPluginClass("controlEntrypoint", "ControlPlugin", libPlugin.BaseControlPlugin, pluginInfo);
|
|
}
|
|
|
|
module.exports = {
|
|
loadPluginInfos,
|
|
loadControllerPluginClass,
|
|
loadInstancePluginClass,
|
|
loadControlPluginClass,
|
|
};
|