From 5dbfff9bfd99bb09302637906a9216a9fbae553a Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 26 Nov 2024 09:23:09 +0000 Subject: [PATCH] Support for monorepo autodiscovery --- packages/lib/src/load_plugin_list.ts | 37 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/packages/lib/src/load_plugin_list.ts b/packages/lib/src/load_plugin_list.ts index d0323ae4..c3b97f97 100644 --- a/packages/lib/src/load_plugin_list.ts +++ b/packages/lib/src/load_plugin_list.ts @@ -20,6 +20,9 @@ async function findLocalPlugins(pluginList: Map, pluginListPath: ]; let has_changed = 0; + const ignoredFolders = new Set(["node_modules", ".git", "dist", "build", "disabled"]); + const maxDepth = 3; + for (const pluginFolder of pluginFolders) { let pluginNames; try { @@ -32,28 +35,36 @@ async function findLocalPlugins(pluginList: Map, pluginListPath: throw err; } for (const pluginName of pluginNames) { - const pluginPath = path.resolve(pluginFolder, pluginName); + const pluginPath = path.join(pluginFolder, pluginName); if (pluginList.has(pluginName)) { // If the one in the list is not the same as this one, warn - if (pluginList.get(pluginName) !== pluginPath) { + if (pluginList.get(pluginName) !== path.resolve(pluginPath)) { logger.warn(`${pluginName} is already in the plugin list, but with a different path - using ${pluginList.get(pluginName)}`); } continue; } const stats = await fs.stat(pluginPath); - if (!stats.isDirectory()) { - continue; - } + if (stats.isDirectory()) { + // Check for package.json in current directory + const packageJsonPath = path.join(pluginPath, "package.json"); + if (await fs.exists(packageJsonPath)) { + // Read and parse package.json + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, { encoding: "utf8" })); - // Check that the plugin has a package.json file - const packageJsonPath = path.resolve(pluginPath, "package.json"); - if (!(await fs.exists(packageJsonPath))) { - continue; - } + // Check if package has the clusterio-plugin keyword + if (packageJson.keywords?.includes("clusterio-plugin")) { + pluginList.set(pluginName, path.resolve(pluginPath)); + logger.info(`Added ${pluginName} from ${pluginFolder}`); + has_changed += 1; + continue; + } + } - pluginList.set(pluginName, pluginPath); - logger.info(`Added ${pluginName} from ${pluginFolder}`); - has_changed += 1; + if (pluginPath.split(path.sep).length < maxDepth && !ignoredFolders.has(pluginName)) { + logger.warn(`Possible monorepo detected, scanning ${pluginPath} for clusterio-plugins`); + pluginFolders.push(pluginPath); + } + } } }