From 427acfe82ebfb66c21e5c2f61f1d352993ac7736 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 26 Nov 2024 10:13:36 +0000 Subject: [PATCH] Refactor, fix linting issues --- packages/lib/src/load_plugin_list.ts | 42 ++++++++++++++++++---------- test/lib/plugin_loader.js | 13 ++++----- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/packages/lib/src/load_plugin_list.ts b/packages/lib/src/load_plugin_list.ts index 29279a64..826a667d 100644 --- a/packages/lib/src/load_plugin_list.ts +++ b/packages/lib/src/load_plugin_list.ts @@ -39,25 +39,21 @@ async function findLocalPlugins(pluginList: Map, pluginListPath: if (pluginList.has(pluginName)) { // If the one in the list is not the same as this one, warn 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)}`); + 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()) { - // 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 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; - } + // Check for package.json and plugin keyword + if (await checkPackageJson(pluginPath)) { + pluginList.set(pluginName, path.resolve(pluginPath)); + logger.info(`Added ${pluginName} from ${pluginFolder}`); + has_changed += 1; + continue; } if (pluginPath.split(path.sep).length < maxDepth && !ignoredFolders.has(pluginName)) { @@ -168,3 +164,21 @@ export async function loadPluginList(pluginListPath: string, findLocal = true): return pluginList; } + +/** + * Helper function to check if a package.json file exists and contains the clusterio-plugin keyword + * @param pluginPath - Path to the plugin directory + * @returns Promise that resolves to true if package.json exists and has clusterio-plugin keyword + */ +async function checkPackageJson(pluginPath: string): Promise { + const packageJsonPath = path.join(pluginPath, "package.json"); + if (!await fs.exists(packageJsonPath)) { + return false; + } + + // Read and parse package.json + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, { encoding: "utf8" })); + + // Check if package has the clusterio-plugin keyword + return packageJson.keywords?.includes("clusterio-plugin"); +} diff --git a/test/lib/plugin_loader.js b/test/lib/plugin_loader.js index 22131f5d..8cca497a 100644 --- a/test/lib/plugin_loader.js +++ b/test/lib/plugin_loader.js @@ -133,7 +133,7 @@ describe("lib/plugin_loader", function() { JSON.stringify({ name: path.basename(pluginPath), version: "0.0.1", - keywords: ["clusterio-plugin"] + keywords: ["clusterio-plugin"], }) ); } @@ -149,8 +149,8 @@ describe("lib/plugin_loader", function() { path.join(baseDir, "package.json"), JSON.stringify({ dependencies: { - "npm-plugin": "^1.0.0" - } + "npm-plugin": "^1.0.0", + }, }) ); @@ -176,14 +176,13 @@ describe("lib/plugin_loader", function() { it("should load existing plugin list", async function() { const existingList = new Map([["test", "/test/path"]]); await fs.outputFile(pluginListPath, JSON.stringify([...existingList])); - const pluginList = await lib.loadPluginList(pluginListPath, false); - assert.ok(pluginList.has("test")); - assert.strictEqual(pluginList.get("test"), "/test/path"); + const loadedPlugins = await lib.loadPluginList(pluginListPath, false); + assert.ok(loadedPlugins.has("test")); + assert.strictEqual(loadedPlugins.get("test"), "/test/path"); }); after(async function() { process.chdir(old_cwd); - console.log(baseDir, process.cwd()); await fs.remove(baseDir); }); });