Add plugin install command to controller, host and ctl

Installing a plugin used to take two steps, npm install followed by plugin
add, and both silently did their work in whatever directory they were run
from. The new plugin install <package> subcommand runs npm install --save
and then discovers the plugin through the existing loadPluginList logic,
so the plugin list is updated in the same step.

The command refuses to run outside a Clusterio installation, detected by
the package.json the installer writes or one that depends on a Clusterio
package. plugin add with a bare package name warns in the same situation.

Reinstalling a plugin that is already listed reports it as such instead of
failing. Installing a package without the clusterio-plugin keyword fails.

Fixes #1008

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
bbassieandClaude Fable 5.1 committed 2026-09-10 00:36:47 +00:00
1 parent 7987a5d925
commit 5606d5d553
6 files changed
+292 -24

No files matched your search

+111 -1
View File
@@ -2,13 +2,16 @@
* Implementation of commands shared between controller/host/ctl.
* @module lib/shared_commands
*/
import fs from "fs/promises";
import path from "path";
import { spawn } from "child_process";
import * as libConfig from "./config";
import * as libFileOps from "./file_ops";
import { logger } from "./logging";
import * as libHelpers from "./helpers";
import { LockFile } from "./LockFile";
import { loadPluginList } from "./load_plugin_list";
function print(...content: any[]) {
@@ -26,6 +29,7 @@ function print(...content: any[]) {
*/
export function pluginCommand(yargs: any) {
yargs
.command("install <package>", "Install plugin from npm and add it")
.command("add <path>", "Add plugin by require path")
.command("remove <name>", "Remove plugin by name")
.command("list", "List all plugins and their path")
@@ -35,6 +39,65 @@ export function pluginCommand(yargs: any) {
;
}
const corePackages = ["@clusterio/controller", "@clusterio/host", "@clusterio/ctl"];
/**
* Check if the current directory is a Clusterio installation
*
* A directory counts as an installation if it has a package.json created
* by the installer or one that depends on a Clusterio package.
*/
export async function isClusterioInstall(dir = process.cwd()) {
let packageJson: { name?: string, dependencies?: Record<string, string> };
try {
packageJson = JSON.parse(await fs.readFile(path.join(dir, "package.json"), "utf8"));
} catch (err: any) {
if (err.code === "ENOENT") {
return false;
}
throw err;
}
if (packageJson.name === "clusterio-install") {
return true;
}
return corePackages.some(name => packageJson.dependencies?.[name] !== undefined);
}
async function readDependencies(): Promise<Record<string, string>> {
const packageJson = JSON.parse(await fs.readFile("package.json", "utf8"));
return packageJson.dependencies ?? {};
}
async function packageNameFromSpec(packageSpec: string) {
try {
const packageJson = JSON.parse(await fs.readFile(path.join(packageSpec, "package.json"), "utf8"));
if (typeof packageJson.name === "string") {
return packageJson.name;
}
} catch {
// Not a local directory, treat it as name@version
}
return packageSpec.replace(/(?!^)@.*$/, "");
}
function npmInstall(packageSpec: string) {
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
return new Promise<void>((resolve, reject) => {
const child = spawn(npm, ["install", "--save", "--no-audit", "--no-fund", packageSpec], {
stdio: "inherit",
shell: process.platform === "win32",
});
child.on("error", reject);
child.on("exit", code => {
if (code === 0) {
resolve();
} else {
reject(new Error(`npm install exited with code ${code}`));
}
});
});
}
/**
* Handle yargs plugin command
*
@@ -50,11 +113,58 @@ export async function handlePluginCommand(
pluginListPath: string
) {
let command = (args._ as string[])[1];
const wrongDirHint = `${process.cwd()} is not a Clusterio installation, run this command from the directory ` +
"Clusterio was installed in";
if (command === "add") {
if (command === "install") {
if (!await isClusterioInstall()) {
logger.error(wrongDirHint);
process.exitCode = 1;
return;
}
const packageSpec = args.package as string;
const depsBefore = await readDependencies();
try {
await npmInstall(packageSpec);
} catch (err: any) {
logger.error(`Failed to install ${packageSpec}: ${err.message}`);
process.exitCode = 1;
return;
}
const depsAfter = await readDependencies();
const newPluginList = await loadPluginList(pluginListPath);
const added = [...newPluginList.keys()].filter(name => !pluginList.has(name));
for (const name of added) {
pluginList.set(name, newPluginList.get(name)!);
print(`Installed ${name}`);
}
if (added.length) {
return;
}
// Nothing new, either the plugin was already in the list or the package is not a plugin.
let packageNames = Object.keys(depsAfter).filter(name => depsAfter[name] !== depsBefore[name]);
if (!packageNames.length) {
packageNames = [await packageNameFromSpec(packageSpec)];
}
const existing = [...pluginList].filter(([, requirePath]) => packageNames.includes(requirePath));
if (existing.length) {
for (const [name] of existing) {
print(`${name} is already installed`);
}
} else {
logger.error(`${packageSpec} was installed but did not provide any plugins`);
process.exitCode = 1;
}
} else if (command === "add") {
let pluginPath = args.path as string;
if (/^\.\.?[\/\\]/.test(pluginPath)) {
pluginPath = path.resolve(pluginPath);
} else if (!path.isAbsolute(pluginPath) && !await isClusterioInstall()) {
logger.warn(wrongDirHint);
}
let pluginInfo: { name: string };