Refactor, fix linting issues

This commit is contained in:
Danielv123 committed 2024-11-26 10:18:54 +00:00
1 parent 79c4bf1441
commit 427acfe82e
2 files changed
+34 -21

No files matched your search

+28 -14
View File
@@ -39,25 +39,21 @@ async function findLocalPlugins(pluginList: Map<string, string>, 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<boolean> {
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");
}
+6 -7
View File
@@ -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);
});
});