feat(lib): discover transitive npm plugins

This commit is contained in:
Loi Nguyen committed 2026-07-26 13:23:20 +07:00
1 parent 5e0ab3423f
commit 40a6142e3e
2 files changed
+79 -13

No files matched your search

+54 -7
View File
@@ -73,6 +73,27 @@ function getPluginName(requireSpec: string) {
return context.pluginInfo.name;
}
async function findNpmPackagePath(packageName: string, parentPath: string): Promise<string | undefined> {
let currentPath = parentPath;
while (true) {
if (path.basename(currentPath) !== "node_modules") {
const packagePath = path.join(currentPath, "node_modules", packageName);
try {
await fs.access(path.join(packagePath, "package.json"));
return packagePath;
} catch {
// Continue searching parent node_modules directories.
}
}
const nextPath = path.dirname(currentPath);
if (nextPath === currentPath) {
return undefined;
}
currentPath = nextPath;
}
}
/**
* Find NPM packages in the root package.json that satisfies the requirements for plugins.
* Adds discovered plugins to the plugin list.
@@ -98,13 +119,27 @@ async function findNpmPlugins(pluginList: Map<string, string>): Promise<boolean>
}
let hasChanged = 0;
for (const [packageName, packageVersion] of Object.entries(dependencies)) {
if ([...pluginList.values()].includes(packageName)) {
continue; // This npm module is already in the plugin list
const pendingPackages = Object.entries(dependencies).map(([packageName, packageVersion]) => ({
packageName,
packageVersion,
parentPath: process.cwd(),
isRootDependency: true,
}));
const visitedPackages = new Set<string>();
while (pendingPackages.length) {
const { packageName, packageVersion, parentPath, isRootDependency } = pendingPackages.shift()!;
const packagePath = await findNpmPackagePath(packageName, parentPath);
if (!packagePath) {
continue;
}
// Find the package in node_modules and read the package.json to check if it's a clusterio-plugin
if (await checkPackageJson(path.resolve("node_modules", packageName))) {
const pluginName = getPluginName(path.resolve("node_modules", packageName));
if (visitedPackages.has(packagePath)) {
continue;
}
visitedPackages.add(packagePath);
if (await checkPackageJson(packagePath)) {
const pluginName = getPluginName(packagePath);
if (pluginList.has(pluginName)) {
logger.warn(
`${pluginName} provided by ${packageName}@${packageVersion} is already in the plugin list, ` +
@@ -115,7 +150,19 @@ async function findNpmPlugins(pluginList: Map<string, string>): Promise<boolean>
logger.info(`Added ${pluginName} from NPM`);
hasChanged += 1;
}
} else if (![
const packageJson = JSON.parse(
await fs.readFile(path.join(packagePath, "package.json"), { encoding: "utf8" })
);
for (const [dependencyName, dependencyVersion] of Object.entries(packageJson.dependencies ?? {})) {
pendingPackages.push({
packageName: dependencyName,
packageVersion: dependencyVersion,
parentPath: packagePath,
isRootDependency: false,
});
}
} else if (isRootDependency && ![
"@clusterio/controller",
"@clusterio/ctl",
"@clusterio/host",
+25 -6
View File
@@ -124,12 +124,16 @@ describe("lib/plugin_loader", function() {
const npmPluginPath = path.join(baseDir, "node_modules", "npm-plugin");
const monorepoPluginPath = path.join(baseDir, "external_plugins", "monorepo", "monorepo-plugin");
const monorepoPluginPathAbs = path.resolve(monorepoPluginPath);
const transitivePluginPath = path.join(baseDir, "node_modules", "transitive-plugin");
const hiddenPluginPath = path.join(
baseDir, "node_modules", "not-a-plugin", "node_modules", "hidden-plugin"
);
const notAPluginPath = path.join(baseDir, "node_modules", "not-a-plugin");
let pluginList;
before(async function() {
// Setup test plugins
async function writePlugin(pluginPath, name) {
async function writePlugin(pluginPath, name, dependencies = {}, packageJsonFields = {}) {
await fs.mkdir(pluginPath, { recursive: true });
await fs.writeFile(
path.join(pluginPath, "index.js"),
@@ -141,6 +145,8 @@ describe("lib/plugin_loader", function() {
name: path.basename(pluginPath),
version: "0.0.1",
keywords: ["clusterio-plugin"],
dependencies,
...packageJsonFields,
})
);
}
@@ -150,7 +156,13 @@ describe("lib/plugin_loader", function() {
// Create external plugin
await writePlugin(externalPluginPath, "external_plugin");
// Create npm plugin
await writePlugin(npmPluginPath, "npm");
await writePlugin(
npmPluginPath,
"npm",
{ "transitive-plugin": "^1.0.0" },
{ exports: "./index.js" },
);
await writePlugin(transitivePluginPath, "transitive", { "npm-plugin": "^1.0.0" });
// Write a monorepo plugin
await writePlugin(monorepoPluginPath, "monorepo-plugin");
// Create root package.json
@@ -165,10 +177,12 @@ describe("lib/plugin_loader", function() {
);
// Create an npm module that is not a plugin
await fs.mkdir(notAPluginPath, { recursive: true });
await fs.writeFile(
path.join(notAPluginPath, "package.json"),
JSON.stringify({ name: "not-a-plugin", version: "1.0.0" })
);
await fs.writeFile(path.join(notAPluginPath, "package.json"), JSON.stringify({
name: "not-a-plugin",
version: "1.0.0",
dependencies: { "hidden-plugin": "^1.0.0" },
}));
await writePlugin(hiddenPluginPath, "hidden");
process.chdir(baseDir);
pluginList = await lib.loadPluginList(pluginListPath, true);
@@ -191,6 +205,11 @@ describe("lib/plugin_loader", function() {
assert.strictEqual(pluginList.get("not-a-plugin"), undefined);
});
it("should discover transitive npm plugins without traversing non-plugin packages", async function() {
assert.strictEqual(pluginList.get("transitive"), "transitive-plugin");
assert.strictEqual(pluginList.get("hidden"), undefined);
});
it("should load existing plugin list", async function() {
const existingList = new Map([["test", "/test/path"]]);
await fs.writeFile(pluginListPath, JSON.stringify([...existingList]));