mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-19 23:33:13 +09:00
Move export for plugin declaration (#555)
Change the expected exported name of plugin declarations to be "plugin" at the main entrypoint instead of the default export. This makes the plugin's code organisation more flexible especially when using CommonJS JavaScript modules as they can export additional symbols from the plugin declaration file.
This commit is contained in:
1 parent
8884116ee9
commit
b0f6c95fca
33 files changed
+71
-62
No files matched your search
@@ -7,8 +7,7 @@ import type { Logger } from "./logging";
|
||||
import type { FieldDefinition } from "./config";
|
||||
import type { PlayerStats } from "./data";
|
||||
|
||||
// TODO Add proper typing for plugins
|
||||
/* Used to define the export of info.ts from plugins */
|
||||
/** Used to define the plugin export in plugins */
|
||||
export type PluginDeclaration = {
|
||||
name: string;
|
||||
title: string;
|
||||
|
||||
@@ -27,12 +27,18 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
let pluginPackage: { name?: string, version: string, main?: string, private?: boolean };
|
||||
|
||||
try {
|
||||
pluginInfo = require(pluginPath).default;
|
||||
pluginInfo = require(pluginPath).plugin;
|
||||
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
|
||||
} catch (err: any) {
|
||||
throw new libErrors.PluginError(pluginName, err);
|
||||
}
|
||||
|
||||
if (typeof pluginInfo !== "object") {
|
||||
throw new libErrors.EnvironmentError(
|
||||
`Expected plugin at ${pluginPath} to export an object named 'plugin' but got ${typeof pluginInfo}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (pluginInfo.name !== pluginName) {
|
||||
throw new libErrors.EnvironmentError(
|
||||
`Expected plugin at ${pluginPath} to be named ${pluginName} but got ${pluginInfo.name}`
|
||||
|
||||
@@ -59,13 +59,19 @@ export async function handlePluginCommand(
|
||||
let pluginInfo: { name: string };
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
pluginInfo = require(pluginPath).default;
|
||||
pluginInfo = require(pluginPath).plugin;
|
||||
} catch (err: any) {
|
||||
logger.error(`Unable to import plugin info from ${args.path}:\n${err.stack}`);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof pluginInfo !== "object") {
|
||||
logger.error("Plugin does not correctly export a 'plugin' object");
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (pluginList.has(pluginInfo.name)) {
|
||||
logger.error(`Plugin with the same ${pluginInfo.name} already exists`);
|
||||
process.exitCode = 1;
|
||||
|
||||
Reference in new issue
Block a user