mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Move the code for loading plugins infos from Node.js packages into a separate module in order to break it out from lib/plugin.js which is also used on the web interface side. This resolves a warning of using an expression with require on the webpack build.
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
/**
|
|
* Helpers for loading plugins in Node.js
|
|
* @module lib/plugin_loader
|
|
*/
|
|
"use strict";
|
|
|
|
const libErrors = require("@clusterio/lib/errors");
|
|
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 {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}`
|
|
);
|
|
}
|
|
|
|
pluginInfo.requirePath = pluginPath;
|
|
pluginInfo.version = pluginPackage.version;
|
|
plugins.push(pluginInfo);
|
|
}
|
|
return plugins;
|
|
}
|
|
|
|
module.exports = {
|
|
loadPluginInfos,
|
|
};
|