diff --git a/packages/create/templates/common/package.json b/packages/create/templates/common/package.json index b05122b3..36e5ad01 100644 --- a/packages/create/templates/common/package.json +++ b/packages/create/templates/common/package.json @@ -8,6 +8,9 @@ //%if !typescript "main": "index.js", //%endif + "keywords": [ + "clusterio-plugin" + ], "scripts": { "prepare": "__prepare__" }, diff --git a/packages/lib/src/load_plugin_list.ts b/packages/lib/src/load_plugin_list.ts index 3545c747..e0eb2b4f 100644 --- a/packages/lib/src/load_plugin_list.ts +++ b/packages/lib/src/load_plugin_list.ts @@ -44,6 +44,37 @@ async function findLocalPlugins(pluginList: Map, pluginListPath: } } +/** + * Find NPM packages in the root package.json that satisfies the requirements for plugins. + */ +async function findNpmPlugins(pluginList: Map, 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; } diff --git a/plugins/global_chat/package.json b/plugins/global_chat/package.json index b8d97aa2..c20e1846 100644 --- a/plugins/global_chat/package.json +++ b/plugins/global_chat/package.json @@ -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" diff --git a/plugins/inventory_sync/package.json b/plugins/inventory_sync/package.json index e184e734..03f31ade 100644 --- a/plugins/inventory_sync/package.json +++ b/plugins/inventory_sync/package.json @@ -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" diff --git a/plugins/player_auth/package.json b/plugins/player_auth/package.json index a7c40a5b..a05ba974 100644 --- a/plugins/player_auth/package.json +++ b/plugins/player_auth/package.json @@ -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" diff --git a/plugins/research_sync/package.json b/plugins/research_sync/package.json index 955874ae..9b553b1d 100644 --- a/plugins/research_sync/package.json +++ b/plugins/research_sync/package.json @@ -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" diff --git a/plugins/statistics_exporter/package.json b/plugins/statistics_exporter/package.json index cb4e9d0c..9737a2a6 100644 --- a/plugins/statistics_exporter/package.json +++ b/plugins/statistics_exporter/package.json @@ -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" diff --git a/plugins/subspace_storage/package.json b/plugins/subspace_storage/package.json index e6de5b57..2825680f 100644 --- a/plugins/subspace_storage/package.json +++ b/plugins/subspace_storage/package.json @@ -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"