mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-26 00:33:12 +09:00
Refactor, fix linting issues
This commit is contained in:
1 parent
79c4bf1441
commit
427acfe82e
2 files changed
+34
-21
No files matched your search
@@ -39,25 +39,21 @@ async function findLocalPlugins(pluginList: Map<string, string>, pluginListPath:
|
|||||||
if (pluginList.has(pluginName)) {
|
if (pluginList.has(pluginName)) {
|
||||||
// If the one in the list is not the same as this one, warn
|
// If the one in the list is not the same as this one, warn
|
||||||
if (pluginList.get(pluginName) !== path.resolve(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)}`);
|
logger.warn(
|
||||||
|
`${pluginName} is already in the plugin list, but with a different path - ` +
|
||||||
|
`using ${pluginList.get(pluginName)}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const stats = await fs.stat(pluginPath);
|
const stats = await fs.stat(pluginPath);
|
||||||
if (stats.isDirectory()) {
|
if (stats.isDirectory()) {
|
||||||
// Check for package.json in current directory
|
// Check for package.json and plugin keyword
|
||||||
const packageJsonPath = path.join(pluginPath, "package.json");
|
if (await checkPackageJson(pluginPath)) {
|
||||||
if (await fs.exists(packageJsonPath)) {
|
pluginList.set(pluginName, path.resolve(pluginPath));
|
||||||
// Read and parse package.json
|
logger.info(`Added ${pluginName} from ${pluginFolder}`);
|
||||||
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, { encoding: "utf8" }));
|
has_changed += 1;
|
||||||
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pluginPath.split(path.sep).length < maxDepth && !ignoredFolders.has(pluginName)) {
|
if (pluginPath.split(path.sep).length < maxDepth && !ignoredFolders.has(pluginName)) {
|
||||||
@@ -168,3 +164,21 @@ export async function loadPluginList(pluginListPath: string, findLocal = true):
|
|||||||
|
|
||||||
return pluginList;
|
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");
|
||||||
|
}
|
||||||
@@ -133,7 +133,7 @@ describe("lib/plugin_loader", function() {
|
|||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
name: path.basename(pluginPath),
|
name: path.basename(pluginPath),
|
||||||
version: "0.0.1",
|
version: "0.0.1",
|
||||||
keywords: ["clusterio-plugin"]
|
keywords: ["clusterio-plugin"],
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -149,8 +149,8 @@ describe("lib/plugin_loader", function() {
|
|||||||
path.join(baseDir, "package.json"),
|
path.join(baseDir, "package.json"),
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
dependencies: {
|
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() {
|
it("should load existing plugin list", async function() {
|
||||||
const existingList = new Map([["test", "/test/path"]]);
|
const existingList = new Map([["test", "/test/path"]]);
|
||||||
await fs.outputFile(pluginListPath, JSON.stringify([...existingList]));
|
await fs.outputFile(pluginListPath, JSON.stringify([...existingList]));
|
||||||
const pluginList = await lib.loadPluginList(pluginListPath, false);
|
const loadedPlugins = await lib.loadPluginList(pluginListPath, false);
|
||||||
assert.ok(pluginList.has("test"));
|
assert.ok(loadedPlugins.has("test"));
|
||||||
assert.strictEqual(pluginList.get("test"), "/test/path");
|
assert.strictEqual(loadedPlugins.get("test"), "/test/path");
|
||||||
});
|
});
|
||||||
|
|
||||||
after(async function() {
|
after(async function() {
|
||||||
process.chdir(old_cwd);
|
process.chdir(old_cwd);
|
||||||
console.log(baseDir, process.cwd());
|
|
||||||
await fs.remove(baseDir);
|
await fs.remove(baseDir);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user