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");
}