mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 07:30:41 +09:00
Add typed hook system for plugins
Replace invokeHook and the per plugin on* methods with AsyncHook collections on the controller, host, instance, clusterioctl and web control objects. Plugin entrypoints export a default function that receives a load context and attaches handlers to the hooks it needs. Class exports still load through fromContext with a deprecation warning. The create templates and docs use the entrypoint function. This is clusterio/clusterio#921 recreated on top of master after the ESM migration and the PluginDeclaration permissions change. Closes #730 Co-Authored-By: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c03ac15ab
commit
7122cbccf8
64 files changed
+1878
-1184
No files matched your search
@@ -1,38 +1,18 @@
|
||||
import type { CommandTree, Logger, PluginNodeEnvInfo } from "@clusterio/lib";
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
export type CtlPluginContext = lib.PluginLoadContext<{
|
||||
hooks: CtlHooks
|
||||
}>;
|
||||
|
||||
/**
|
||||
* Base class for clusterioctl plugins
|
||||
*
|
||||
* Ctl plugins are subclasses of this class which get instantiated by
|
||||
* clusterioctl in order to extend its functionallity. To be discovered the
|
||||
* class must be exported under the name `CtlPlugin` in the module
|
||||
* specified by the `ctlEntrypoint` in the plugin's `plugin` export.
|
||||
* Collection of clusterioctl plugin hooks
|
||||
*/
|
||||
export default class BaseCtlPlugin {
|
||||
/**
|
||||
* Logger for this plugin
|
||||
*
|
||||
* Instance of winston Logger for sending log messages from this
|
||||
* plugin. Supported methods and their corresponding log levels are
|
||||
* `error`, `warn`, `audit`, `info` and `verbose`.
|
||||
*/
|
||||
logger: Logger;
|
||||
|
||||
constructor(
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
public info: PluginNodeEnvInfo,
|
||||
logger: Logger,
|
||||
) {
|
||||
this.logger = logger.child({ plugin: this.info.name }) as unknown as Logger;
|
||||
export class CtlHooks extends lib.AsyncHookCollection {
|
||||
constructor(logger: lib.Logger) {
|
||||
super(logger);
|
||||
this.addCommands = this.newHook();
|
||||
}
|
||||
|
||||
/**
|
||||
* Called immediately after the class is instantiated
|
||||
*/
|
||||
async init() { }
|
||||
|
||||
/**
|
||||
* Called to add commands to the command line interface.
|
||||
*
|
||||
@@ -43,5 +23,47 @@ export default class BaseCtlPlugin {
|
||||
* @param rootCommand -
|
||||
* Root of the clusterioctl command tree.
|
||||
*/
|
||||
async addCommands(rootCommand: CommandTree) { }
|
||||
readonly addCommands: lib.AsyncHook<[rootCommand: lib.CommandTree]>;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
||||
export interface BaseCtlPlugin {
|
||||
addCommands?(rootCommand: lib.CommandTree): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for clusterioctl plugins
|
||||
*
|
||||
* Ctl plugins are subclasses of this class which get instantiated by
|
||||
* clusterioctl in order to extend its functionallity. To be discovered the
|
||||
* class must be exported under the name `CtlPlugin` in the module
|
||||
* specified by the `ctlEntrypoint` in the plugin's `plugin` export.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
||||
export class BaseCtlPlugin {
|
||||
constructor(
|
||||
/**
|
||||
* The plugin's own info module
|
||||
*/
|
||||
public info: lib.PluginNodeEnvInfo,
|
||||
public logger: lib.Logger,
|
||||
private hooks: CtlHooks,
|
||||
) {
|
||||
if (this.addCommands) {
|
||||
hooks.addCommands.attach(info.name, this.addCommands.bind(this));
|
||||
}
|
||||
}
|
||||
|
||||
static fromContext(context: CtlPluginContext) {
|
||||
return new this(context.plugin, context.logger, context.hooks);
|
||||
}
|
||||
|
||||
detachHooks() {
|
||||
this.hooks.detachAll(this.info.name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called immediately after the class is instantiated
|
||||
*/
|
||||
async init() {}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import type { Argv } from "yargs";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
import type BaseCtlPlugin from "./BaseCtlPlugin.js";
|
||||
import { controllerCommands } from "./commands_controller.js";
|
||||
import { hostCommands } from "./commands_host.js";
|
||||
import { instanceCommands } from "./commands_instance.js";
|
||||
@@ -12,8 +11,9 @@ import { roleCommands } from "./commands_role.js";
|
||||
import { userCommands } from "./commands_user.js";
|
||||
import { logCommands } from "./commands_log.js";
|
||||
import { debugCommands } from "./commands_debug.js";
|
||||
import type { CtlHooks } from "./BaseCtlPlugin.js";
|
||||
|
||||
export async function registerCommands(ctlPlugins: Map<string, BaseCtlPlugin>, yargs: Argv) {
|
||||
export async function registerCommands(ctlHooks: CtlHooks, yargs: Argv) {
|
||||
const rootCommands = new lib.CommandTree({ name: "clusterioctl", description: "Manage cluster" });
|
||||
rootCommands.add(controllerCommands);
|
||||
rootCommands.add(hostCommands);
|
||||
@@ -26,9 +26,7 @@ export async function registerCommands(ctlPlugins: Map<string, BaseCtlPlugin>, y
|
||||
rootCommands.add(logCommands);
|
||||
rootCommands.add(debugCommands);
|
||||
|
||||
for (let controlPlugin of ctlPlugins.values()) {
|
||||
await controlPlugin.addCommands(rootCommands);
|
||||
}
|
||||
await ctlHooks.addCommands.invoke(rootCommands);
|
||||
|
||||
for (let [name, command] of rootCommands.subCommands) {
|
||||
if (name === command.name) {
|
||||
|
||||
Reference in new issue
Block a user