mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-27 00:37:14 +09:00
Move loadPluginInfos out of lib/plugin.js
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.
This commit is contained in:
1 parent
df94ff1d8d
commit
0494858886
7 files changed
+117
-98
No files matched your search
+2
-1
@@ -17,6 +17,7 @@ const libLink = require("@clusterio/lib/link");
|
|||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const libConfig = require("@clusterio/lib/config");
|
const libConfig = require("@clusterio/lib/config");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
|
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
||||||
const libCommand = require("@clusterio/lib/command");
|
const libCommand = require("@clusterio/lib/command");
|
||||||
const libSharedCommands = require("@clusterio/lib/shared_commands");
|
const libSharedCommands = require("@clusterio/lib/shared_commands");
|
||||||
|
|
||||||
@@ -628,7 +629,7 @@ async function startControl() {
|
|||||||
rootCommands.add(debugCommands);
|
rootCommands.add(debugCommands);
|
||||||
|
|
||||||
console.log("Loading Plugin info");
|
console.log("Loading Plugin info");
|
||||||
let pluginInfos = await libPlugin.loadPluginInfos(pluginList);
|
let pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
||||||
libConfig.registerPluginConfigGroups(pluginInfos);
|
libConfig.registerPluginConfigGroups(pluginInfos);
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,6 @@
|
|||||||
* @module lib/plugin
|
* @module lib/plugin
|
||||||
*/
|
*/
|
||||||
"use strict";
|
"use strict";
|
||||||
const path = require("path");
|
|
||||||
|
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -386,45 +383,6 @@ class BaseControlPlugin {
|
|||||||
async addCommands(rootCommand) { }
|
async addCommands(rootCommand) { }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attach plugin messages
|
* Attach plugin messages
|
||||||
*
|
*
|
||||||
@@ -485,7 +443,6 @@ module.exports = {
|
|||||||
BaseMasterPlugin,
|
BaseMasterPlugin,
|
||||||
BaseControlPlugin,
|
BaseControlPlugin,
|
||||||
|
|
||||||
loadPluginInfos,
|
|
||||||
attachPluginMessages,
|
attachPluginMessages,
|
||||||
invokeHook,
|
invokeHook,
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* 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,
|
||||||
|
};
|
||||||
@@ -49,6 +49,7 @@ const libSchema = require("@clusterio/lib/schema");
|
|||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
|
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
||||||
const libPrometheus = require("@clusterio/lib/prometheus");
|
const libPrometheus = require("@clusterio/lib/prometheus");
|
||||||
const libConfig = require("@clusterio/lib/config");
|
const libConfig = require("@clusterio/lib/config");
|
||||||
const libUsers = require("@clusterio/lib/users");
|
const libUsers = require("@clusterio/lib/users");
|
||||||
@@ -1611,7 +1612,7 @@ async function startServer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log("Loading Plugin info");
|
console.log("Loading Plugin info");
|
||||||
pluginInfos = await libPlugin.loadPluginInfos(pluginList);
|
pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
||||||
libConfig.registerPluginConfigGroups(pluginInfos);
|
libConfig.registerPluginConfigGroups(pluginInfos);
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ const libFileOps = require("@clusterio/lib/file_ops");
|
|||||||
const libFactorio = require("@clusterio/lib/factorio");
|
const libFactorio = require("@clusterio/lib/factorio");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
const libPlugin = require("@clusterio/lib/plugin");
|
const libPlugin = require("@clusterio/lib/plugin");
|
||||||
|
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
||||||
const libErrors = require("@clusterio/lib/errors");
|
const libErrors = require("@clusterio/lib/errors");
|
||||||
const libPrometheus = require("@clusterio/lib/prometheus");
|
const libPrometheus = require("@clusterio/lib/prometheus");
|
||||||
const libLuaTools = require("@clusterio/lib/lua_tools");
|
const libLuaTools = require("@clusterio/lib/lua_tools");
|
||||||
@@ -1419,7 +1420,7 @@ async function startSlave() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log("Loading Plugin info");
|
console.log("Loading Plugin info");
|
||||||
let pluginInfos = await libPlugin.loadPluginInfos(pluginList);
|
let pluginInfos = await libPluginLoader.loadPluginInfos(pluginList);
|
||||||
libConfig.registerPluginConfigGroups(pluginInfos);
|
libConfig.registerPluginConfigGroups(pluginInfos);
|
||||||
libConfig.finalizeConfigs();
|
libConfig.finalizeConfigs();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
const assert = require("assert").strict;
|
const assert = require("assert").strict;
|
||||||
const fs = require("fs-extra");
|
|
||||||
const path = require("path");
|
|
||||||
|
|
||||||
const mock = require("../mock");
|
const mock = require("../mock");
|
||||||
const libLink = require("@clusterio/lib/link");
|
const libLink = require("@clusterio/lib/link");
|
||||||
@@ -41,56 +39,6 @@ describe("lib/plugin", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("loadPluginInfos()", function() {
|
|
||||||
let baseDir = path.join("temp", "test", "plugin");
|
|
||||||
let missingPlugin = path.join(baseDir, "missing_plugin");
|
|
||||||
let testPlugin = path.join(baseDir, "test_plugin");
|
|
||||||
let brokenPlugin = path.join(baseDir, "broken_plugin");
|
|
||||||
let invalidPlugin = path.join(baseDir, "invalid_plugin");
|
|
||||||
before(async function() {
|
|
||||||
async function writePlugin(pluginPath, infoName) {
|
|
||||||
await fs.outputFile(
|
|
||||||
path.join(pluginPath, "info.js"),
|
|
||||||
`module.exports = { name: "${infoName}" };`
|
|
||||||
);
|
|
||||||
await fs.outputFile(
|
|
||||||
path.join(pluginPath, "package.json"),
|
|
||||||
'{ "version": "0.0.1" }'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
await writePlugin(testPlugin, "test");
|
|
||||||
await writePlugin(brokenPlugin, "broken");
|
|
||||||
await fs.outputFile(path.join(brokenPlugin, "info.js"), "Syntax Error");
|
|
||||||
await writePlugin(invalidPlugin, "wrong");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should throw on missing plugin", async function() {
|
|
||||||
await assert.rejects(
|
|
||||||
libPlugin.loadPluginInfos(new Map([["missing", missingPlugin]]), []),
|
|
||||||
new RegExp(`^Error: PluginError: Cannot find module '${missingPlugin}/info'`)
|
|
||||||
);
|
|
||||||
});
|
|
||||||
it("should load test plugin", async function() {
|
|
||||||
assert.deepEqual(
|
|
||||||
await libPlugin.loadPluginInfos(new Map([["test", path.resolve(testPlugin)]])),
|
|
||||||
[{ name: "test", version: "0.0.1", requirePath: path.resolve(testPlugin) }]
|
|
||||||
);
|
|
||||||
});
|
|
||||||
it("should reject on broken plugin", async function() {
|
|
||||||
await assert.rejects(
|
|
||||||
libPlugin.loadPluginInfos(new Map([["broken", path.resolve(brokenPlugin)]])),
|
|
||||||
{ message: "PluginError: Unexpected identifier" }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
it("should reject on invalid plugin", async function() {
|
|
||||||
await assert.rejects(
|
|
||||||
libPlugin.loadPluginInfos(new Map([["invalid", path.resolve(invalidPlugin)]])),
|
|
||||||
{ message: `Expected plugin at ${path.resolve(invalidPlugin)} to be named invalid but got wrong` }
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("attachPluginMessages()", function() {
|
describe("attachPluginMessages()", function() {
|
||||||
let mockLink = new libLink.Link("source", "target", new mock.MockConnector());
|
let mockLink = new libLink.Link("source", "target", new mock.MockConnector());
|
||||||
let mockEvent = new libLink.Event({ type: "test:test", links: ["target-source"] });
|
let mockEvent = new libLink.Event({ type: "test:test", links: ["target-source"] });
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
"use strict";
|
||||||
|
const assert = require("assert").strict;
|
||||||
|
const fs = require("fs-extra");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
|
const libPluginLoader = require("@clusterio/lib/plugin_loader");
|
||||||
|
|
||||||
|
|
||||||
|
describe("lib/plugin_loader", function() {
|
||||||
|
describe("loadPluginInfos()", function() {
|
||||||
|
let baseDir = path.join("temp", "test", "plugin");
|
||||||
|
let missingPlugin = path.join(baseDir, "missing_plugin");
|
||||||
|
let testPlugin = path.join(baseDir, "test_plugin");
|
||||||
|
let brokenPlugin = path.join(baseDir, "broken_plugin");
|
||||||
|
let invalidPlugin = path.join(baseDir, "invalid_plugin");
|
||||||
|
before(async function() {
|
||||||
|
async function writePlugin(pluginPath, infoName) {
|
||||||
|
await fs.outputFile(
|
||||||
|
path.join(pluginPath, "info.js"),
|
||||||
|
`module.exports = { name: "${infoName}" };`
|
||||||
|
);
|
||||||
|
await fs.outputFile(
|
||||||
|
path.join(pluginPath, "package.json"),
|
||||||
|
'{ "version": "0.0.1" }'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await writePlugin(testPlugin, "test");
|
||||||
|
await writePlugin(brokenPlugin, "broken");
|
||||||
|
await fs.outputFile(path.join(brokenPlugin, "info.js"), "Syntax Error");
|
||||||
|
await writePlugin(invalidPlugin, "wrong");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw on missing plugin", async function() {
|
||||||
|
await assert.rejects(
|
||||||
|
libPluginLoader.loadPluginInfos(new Map([["missing", missingPlugin]]), []),
|
||||||
|
new RegExp(`^Error: PluginError: Cannot find module '${missingPlugin}/info'`)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("should load test plugin", async function() {
|
||||||
|
assert.deepEqual(
|
||||||
|
await libPluginLoader.loadPluginInfos(new Map([["test", path.resolve(testPlugin)]])),
|
||||||
|
[{ name: "test", version: "0.0.1", requirePath: path.resolve(testPlugin) }]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("should reject on broken plugin", async function() {
|
||||||
|
await assert.rejects(
|
||||||
|
libPluginLoader.loadPluginInfos(new Map([["broken", path.resolve(brokenPlugin)]])),
|
||||||
|
{ message: "PluginError: Unexpected identifier" }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("should reject on invalid plugin", async function() {
|
||||||
|
await assert.rejects(
|
||||||
|
libPluginLoader.loadPluginInfos(new Map([["invalid", path.resolve(invalidPlugin)]])),
|
||||||
|
{ message: `Expected plugin at ${path.resolve(invalidPlugin)} to be named invalid but got wrong` }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user