Autodiscover plugins in dependencies with keyword "clusterio-plugin"

This commit is contained in:
Danielv123 committed 2024-11-25 17:23:03 +00:00
1 parent 636777d935
commit 397dc6a5a6
8 files changed
+55

No files matched your search

@@ -8,6 +8,9 @@
//%if !typescript
"main": "index.js",
//%endif
"keywords": [
"clusterio-plugin"
],
"scripts": {
"prepare": "__prepare__"
},
+34
View File
@@ -44,6 +44,37 @@ async function findLocalPlugins(pluginList: Map<string, string>, pluginListPath:
}
}
/**
* Find NPM packages in the root package.json that satisfies the requirements for plugins.
*/
async function findNpmPlugins(pluginList: Map<string, string>, pluginListPath: string) {
const rootPackageJson = JSON.parse(await fs.readFile(path.resolve(process.cwd(), "package.json"), { encoding: "utf8" }));
const dependencies = rootPackageJson.dependencies;
let changed = 0;
for (const [pluginName, pluginVersion] of Object.entries(dependencies)) {
if (pluginList.has(pluginName)) {
console.log(`${pluginName} already exists`);
continue;
}
// Find the package in node_modules and read the package.json
const packageJsonPath = path.resolve(process.cwd(), "node_modules", pluginName, "package.json");
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, { encoding: "utf8" }));
// Check if the package has the "clusterio-plugin" keyword
if (packageJson.keywords?.includes("clusterio-plugin")) {
const pluginInfo = require(path.resolve(process.cwd(), "node_modules", pluginName)).plugin;
pluginList.set(pluginInfo.name, pluginName);
logger.info(`Added ${pluginInfo.name} from NPM`);
changed++;
} else {
logger.info(`${pluginName}@${pluginVersion} is not a clusterio-plugin`);
}
}
if (changed > 0) {
await libFileOps.safeOutputFile(pluginListPath, JSON.stringify([...pluginList], null, "\t"));
}
}
/**
* Loads the plugin list from the specified file and optionally discovers local plugins
*
@@ -70,5 +101,8 @@ export async function loadPluginList(pluginListPath: string, findLocal = true):
await findLocalPlugins(pluginList, pluginListPath);
}
// Optionally discover NPM plugins
await findNpmPlugins(pluginList, pluginListPath);
return pluginList;
}
+3
View File
@@ -7,6 +7,9 @@
"license": "MIT",
"repository": "clusterio/clusterio",
"main": "dist/node/index.js",
"keywords": [
"clusterio-plugin"
],
"scripts": {
"prepare": "tsc --build && webpack-cli --env production",
"test": "echo \"Error: run tests from root\" && exit 1"
+3
View File
@@ -7,6 +7,9 @@
"license": "MIT",
"repository": "clusterio/factorioClusterio",
"main": "dist/node/index.js",
"keywords": [
"clusterio-plugin"
],
"scripts": {
"prepare": "tsc --build && webpack-cli --env production",
"test": "echo \"Error: run tests from root\" && exit 1"
+3
View File
@@ -7,6 +7,9 @@
"license": "MIT",
"repository": "clusterio/clusterio",
"main": "dist/node/index.js",
"keywords": [
"clusterio-plugin"
],
"scripts": {
"prepare": "tsc --build && webpack-cli --env production",
"test": "echo \"Error: run tests from root\" && exit 1"
+3
View File
@@ -7,6 +7,9 @@
"license": "MIT",
"repository": "clusterio/clusterio",
"main": "dist/node/index.js",
"keywords": [
"clusterio-plugin"
],
"scripts": {
"prepare": "tsc --build && webpack-cli --env production",
"test": "echo \"Error: run tests from root\" && exit 1"
+3
View File
@@ -7,6 +7,9 @@
"license": "MIT",
"repository": "clusterio/clusterio",
"main": "dist/node/index.js",
"keywords": [
"clusterio-plugin"
],
"scripts": {
"prepare": "tsc --build && webpack-cli --env production",
"test": "echo \"Error: run tests from root\" && exit 1"
+3
View File
@@ -7,6 +7,9 @@
"license": "MIT",
"repository": "clusterio/clusterio",
"main": "dist/node/index.js",
"keywords": [
"clusterio-plugin"
],
"scripts": {
"prepare": "tsc --build && webpack-cli --env production",
"test": "echo \"Error: run tests from root\" && exit 1"