mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-13 07:04:53 +09:00
Add locally provided plugins by default
This commit is contained in:
1 parent
05ca4eb25e
commit
636777d935
6 files changed
+87
-24
No files matched your search
@@ -317,14 +317,7 @@ async function initialize(): Promise<InitializeParameters> {
|
||||
}
|
||||
|
||||
logger.info(`Loading available plugins from ${args.pluginList}`);
|
||||
let pluginList = new Map();
|
||||
try {
|
||||
pluginList = new Map(JSON.parse(await fs.readFile(args.pluginList, { encoding: "utf8" })));
|
||||
} catch (err: any) {
|
||||
if (err.code !== "ENOENT") {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
let pluginList = await lib.loadPluginList(args.pluginList);
|
||||
|
||||
// If the command is plugin management we don't try to load plugins
|
||||
if (command === "plugin") {
|
||||
|
||||
+1
-8
@@ -188,14 +188,7 @@ async function startControl() {
|
||||
lib.handleUnhandledErrors();
|
||||
|
||||
logger.verbose(`Loading available plugins from ${args.pluginList}`);
|
||||
let pluginList = new Map();
|
||||
try {
|
||||
pluginList = new Map(JSON.parse(await fs.readFile(args.pluginList, "utf8")));
|
||||
} catch (err: any) {
|
||||
if (err.code !== "ENOENT") {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
let pluginList = await lib.loadPluginList(args.pluginList);
|
||||
|
||||
// If the command is plugin management we don't try to load plugins
|
||||
if (args._[0] === "plugin") {
|
||||
|
||||
@@ -147,14 +147,7 @@ async function startHost() {
|
||||
}
|
||||
|
||||
logger.info(`Loading available plugins from ${args.pluginList}`);
|
||||
let pluginList = new Map();
|
||||
try {
|
||||
pluginList = new Map(JSON.parse(await fs.readFile(args.pluginList, "utf8")));
|
||||
} catch (err: any) {
|
||||
if (err.code !== "ENOENT") {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
let pluginList = await lib.loadPluginList(args.pluginList);
|
||||
|
||||
// If the command is plugin management we don't try to load plugins
|
||||
if (command === "plugin") {
|
||||
|
||||
@@ -29,6 +29,7 @@ export * from "./src/system_collectors";
|
||||
export * from "./src/zip_ops";
|
||||
export * from "./src/subscriptions";
|
||||
export * from "./src/datastore";
|
||||
export * from "./src/load_plugin_list";
|
||||
|
||||
export { default as ExponentialBackoff } from "./src/ExponentialBackoff";
|
||||
export { default as ModStore } from "./src/ModStore";
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import fs from "fs-extra";
|
||||
import path from "path";
|
||||
import { logger } from "./logging";
|
||||
import * as libFileOps from "./file_ops";
|
||||
|
||||
/**
|
||||
* Find local plugins to add to the plugin list. This is primarily used during development.
|
||||
*/
|
||||
async function findLocalPlugins(pluginList: Map<string, string>, pluginListPath: string) {
|
||||
// Check folders for plugins
|
||||
const pluginFolders = [
|
||||
path.join(process.cwd(), "plugins"),
|
||||
path.join(process.cwd(), "external_plugins"),
|
||||
];
|
||||
|
||||
let has_changed = 0;
|
||||
for (const pluginFolder of pluginFolders) {
|
||||
const pluginNames = await fs.readdir(pluginFolder);
|
||||
for (const pluginName of pluginNames) {
|
||||
if (pluginList.has(pluginName)) {
|
||||
continue;
|
||||
}
|
||||
const pluginPath = path.resolve(pluginFolder, pluginName);
|
||||
const stats = await fs.stat(pluginPath);
|
||||
if (!stats.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check that the plugin has a package.json file
|
||||
const packageJsonPath = path.resolve(pluginPath, "package.json");
|
||||
if (!(await fs.exists(packageJsonPath))) {
|
||||
continue;
|
||||
}
|
||||
console.log(`Adding ${pluginName} from ${pluginFolder}`);
|
||||
|
||||
pluginList.set(pluginName, pluginPath);
|
||||
logger.info(`Added ${pluginName} from ${pluginFolder}`);
|
||||
has_changed++;
|
||||
}
|
||||
}
|
||||
|
||||
if (has_changed > 0) {
|
||||
await libFileOps.safeOutputFile(pluginListPath, JSON.stringify([...pluginList], null, "\t"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the plugin list from the specified file and optionally discovers local plugins
|
||||
*
|
||||
* @param pluginListPath - Path to the plugin list JSON file
|
||||
* @param findLocal - Whether to look for local plugins in plugins/ and external_plugins/ directories
|
||||
* @returns Map of plugin name to plugin path
|
||||
*/
|
||||
export async function loadPluginList(pluginListPath: string, findLocal = true): Promise<Map<string, string>> {
|
||||
let pluginList = new Map<string, string>();
|
||||
|
||||
// Try to load existing plugin list
|
||||
try {
|
||||
pluginList = new Map(JSON.parse(await fs.readFile(pluginListPath, { encoding: "utf8" })));
|
||||
logger.info(`Loaded ${pluginList.size} plugins from ${pluginListPath}`);
|
||||
} catch (err: any) {
|
||||
if (err.code !== "ENOENT") {
|
||||
throw err;
|
||||
}
|
||||
logger.info(`No existing plugin list found at ${pluginListPath}`);
|
||||
}
|
||||
|
||||
// Optionally discover local plugins
|
||||
if (findLocal) {
|
||||
await findLocalPlugins(pluginList, pluginListPath);
|
||||
}
|
||||
|
||||
return pluginList;
|
||||
}
|
||||
@@ -26,6 +26,15 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
let pluginInfo: libPlugin.PluginNodeEnvInfo;
|
||||
let pluginPackage: { name?: string, version: string, main?: string, private?: boolean };
|
||||
|
||||
// Check if plugin path exists, otherwise remove it
|
||||
try {
|
||||
require.resolve(pluginPath);
|
||||
} catch (err) {
|
||||
logger.warn(`Plugin path ${pluginPath} does not exist, removing ${pluginName}`);
|
||||
pluginList.delete(pluginName);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
pluginInfo = require(pluginPath).plugin;
|
||||
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
|
||||
|
||||
Reference in new issue
Block a user