Move base plugins to their respective packages

Move BaseControllerPlugin to controller/src/BaseControllerPlugin.ts
Move BaseInstancePlugin to host/src/BaseInstancePlugin.ts
Move BaseControlPlugin to ctl/src/BaseCtlPlugin.ts
Move BaseWebPlugin to web_ui/src/BaseWebPlugin.ts

This solves the circular package dependency problem of the base plugins
needing types that are available only in the dependencies to lib.
This commit is contained in:
Hornwitser committed 2023-11-25 18:41:43 +01:00
1 parent 63dc370ea0
commit 808eda0513
51 files changed
+1073 -1060

No files matched your search

+25 -15
View File
@@ -6,6 +6,7 @@
*/
import fs from "fs-extra";
import yargs from "yargs";
import path from "path";
import { version } from "./package.json";
import setBlocking from "set-blocking";
import { strict as assert } from "assert";
@@ -16,6 +17,7 @@ import * as lib from "@clusterio/lib";
import { ConsoleTransport, levels, logger } from "@clusterio/lib";
import * as commands from "./src/commands";
import BaseCtlPlugin from "./src/BaseCtlPlugin";
/**
@@ -56,7 +58,7 @@ export class Control extends lib.Link {
/** Certificate authority used to validate TLS connections to the controller. */
tlsCa?: string;
/** Mapping of plugin names to their instance for loaded plugins. */
plugins: Map<string, lib.BaseControlPlugin>;
plugins: Map<string, BaseCtlPlugin>;
/** Keep the control connection alive after the command completes. */
keepOpen = false;
@@ -64,12 +66,12 @@ export class Control extends lib.Link {
connector: ControlConnector,
controlConfig: lib.ControlConfig,
tlsCa: string | undefined,
controlPlugins: Map<string, lib.BaseControlPlugin>
ctlPlugins: Map<string, BaseCtlPlugin>
) {
super(connector);
this.config = controlConfig;
this.tlsCa = tlsCa;
this.plugins = controlPlugins;
this.plugins = ctlPlugins;
this.handle(lib.LogMessageEvent, this.handleLogMessageEvent.bind(this));
this.handle(lib.DebugWsMessageEvent, this.handleDebugWsMessageEvent.bind(this));
@@ -115,18 +117,23 @@ async function loadPlugins(pluginList: Map<string, string>) {
lib.registerPluginConfigGroups(pluginInfos);
lib.finalizeConfigs();
let controlPlugins = new Map<string, lib.BaseControlPlugin>();
let ctlPlugins = new Map<string, BaseCtlPlugin>();
for (let pluginInfo of pluginInfos) {
if (!pluginInfo.controlEntrypoint) {
if (!pluginInfo.ctlEntrypoint) {
continue;
}
let ControlPluginClass = await lib.loadControlPluginClass(pluginInfo);
let controlPlugin = new ControlPluginClass(pluginInfo, logger);
controlPlugins.set(pluginInfo.name, controlPlugin);
await controlPlugin.init();
let CtlPlugin = await lib.loadPluginClass(
pluginInfo.name,
path.posix.join(pluginInfo.requirePath, pluginInfo.ctlEntrypoint),
"CtlPlugin",
BaseCtlPlugin,
);
let ctlPlugin = new CtlPlugin(pluginInfo, logger);
ctlPlugins.set(pluginInfo.name, ctlPlugin);
await ctlPlugin.init();
}
return controlPlugins;
return ctlPlugins;
}
interface CtlArguments {
@@ -199,10 +206,10 @@ async function startControl() {
}
logger.verbose("Loading Plugins");
let controlPlugins = await loadPlugins(pluginList);
let ctlPlugins = await loadPlugins(pluginList);
// Add all cluster management commands including ones from plugins
let rootCommands = await commands.registerCommands(controlPlugins, yargs);
let rootCommands = await commands.registerCommands(ctlPlugins, yargs);
// Reparse after commands have been added with help and strict checking.
args = yargs
@@ -264,7 +271,7 @@ async function startControl() {
tlsCa,
controlConfig.get("control.controller_token") as string,
);
let control = new Control(controlConnector, controlConfig, tlsCa, controlPlugins);
let control = new Control(controlConnector, controlConfig, tlsCa, ctlPlugins);
try {
await controlConnector.connect();
} catch (err) {
@@ -312,8 +319,7 @@ async function startControl() {
}
}
if (module === require.main) {
export function bootstrap() {
// eslint-disable-next-line no-console
console.warn(`
+==========================================================+
@@ -346,3 +352,7 @@ ${err.stack}`
process.exitCode = 1;
});
}
if (module === require.main) {
bootstrap();
}
+6
View File
@@ -0,0 +1,6 @@
import { bootstrap } from "./ctl";
export { default as BaseCtlPlugin } from "./src/BaseCtlPlugin";
if (module === require.main) {
bootstrap();
}
+1 -1
View File
@@ -4,7 +4,7 @@
"author": "Hornwitser",
"license": "MIT",
"version": "2.0.0-alpha.13",
"main": "dist/ctl.js",
"main": "dist/index.js",
"scripts": {
"prepare": "tsc --build",
"test": "echo \"Error: no test specified\" && exit 1"
+47
View File
@@ -0,0 +1,47 @@
import type { CommandTree, Logger, PluginNodeEnvInfo } from "@clusterio/lib";
/**
* 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 info.js file.
*/
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;
}
/**
* Called immediately after the class is instantiated
*/
async init() { }
/**
* Called to add commands to the command line interface.
*
* Invoked by clusterioctl to let plugins add commands. `rootCommand` is
* the top level command node which the plugin should add its own {@link
* CommandTree} to.
*
* @param rootCommand -
* Root of the clusterioctl command tree.
*/
async addCommands(rootCommand: CommandTree) { }
}
+3 -2
View File
@@ -13,6 +13,7 @@ import type { Argv } from "yargs";
import * as lib from "@clusterio/lib";
import { ConsoleTransport, levels, logger } from "@clusterio/lib";
import type { Control } from "../ctl";
import type BaseCtlPlugin from "./BaseCtlPlugin";
const asTable = asTableModule.configure({ delimiter: " | " });
const finished = util.promisify(stream.finished);
@@ -1722,7 +1723,7 @@ debugCommands.add(new lib.Command({
},
}));
export async function registerCommands(controlPlugins: Map<string, lib.BaseControlPlugin>, yargs: Argv) {
export async function registerCommands(ctlPlugins: Map<string, BaseCtlPlugin>, yargs: Argv) {
const rootCommands = new lib.CommandTree({ name: "clusterioctl", description: "Manage cluster" });
rootCommands.add(controllerCommands);
rootCommands.add(hostCommands);
@@ -1735,7 +1736,7 @@ export async function registerCommands(controlPlugins: Map<string, lib.BaseContr
rootCommands.add(logCommands);
rootCommands.add(debugCommands);
for (let controlPlugin of controlPlugins.values()) {
for (let controlPlugin of ctlPlugins.values()) {
await controlPlugin.addCommands(rootCommands);
}
+1 -1
View File
@@ -3,7 +3,7 @@
"references": [
{ "path": "../lib" },
],
"include": ["package.json", "ctl.ts", "src/**/*.ts"],
"include": ["index.ts", "package.json", "ctl.ts", "src/**/*.ts"],
"compilerOptions": {
"outDir": "dist",
},