mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-27 00:37:14 +09:00
feat(lib): discover transitive npm plugins
This commit is contained in:
1 parent
5e0ab3423f
commit
40a6142e3e
2 files changed
+79
-13
No files matched your search
@@ -73,6 +73,27 @@ function getPluginName(requireSpec: string) {
|
|||||||
return context.pluginInfo.name;
|
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.
|
* Find NPM packages in the root package.json that satisfies the requirements for plugins.
|
||||||
* Adds discovered plugins to the plugin list.
|
* Adds discovered plugins to the plugin list.
|
||||||
@@ -98,13 +119,27 @@ async function findNpmPlugins(pluginList: Map<string, string>): Promise<boolean>
|
|||||||
}
|
}
|
||||||
|
|
||||||
let hasChanged = 0;
|
let hasChanged = 0;
|
||||||
for (const [packageName, packageVersion] of Object.entries(dependencies)) {
|
const pendingPackages = Object.entries(dependencies).map(([packageName, packageVersion]) => ({
|
||||||
if ([...pluginList.values()].includes(packageName)) {
|
packageName,
|
||||||
continue; // This npm module is already in the plugin list
|
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 (visitedPackages.has(packagePath)) {
|
||||||
if (await checkPackageJson(path.resolve("node_modules", packageName))) {
|
continue;
|
||||||
const pluginName = getPluginName(path.resolve("node_modules", packageName));
|
}
|
||||||
|
visitedPackages.add(packagePath);
|
||||||
|
|
||||||
|
if (await checkPackageJson(packagePath)) {
|
||||||
|
const pluginName = getPluginName(packagePath);
|
||||||
if (pluginList.has(pluginName)) {
|
if (pluginList.has(pluginName)) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`${pluginName} provided by ${packageName}@${packageVersion} is already in the plugin list, ` +
|
`${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`);
|
logger.info(`Added ${pluginName} from NPM`);
|
||||||
hasChanged += 1;
|
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/controller",
|
||||||
"@clusterio/ctl",
|
"@clusterio/ctl",
|
||||||
"@clusterio/host",
|
"@clusterio/host",
|
||||||
|
|||||||
@@ -124,12 +124,16 @@ describe("lib/plugin_loader", function() {
|
|||||||
const npmPluginPath = path.join(baseDir, "node_modules", "npm-plugin");
|
const npmPluginPath = path.join(baseDir, "node_modules", "npm-plugin");
|
||||||
const monorepoPluginPath = path.join(baseDir, "external_plugins", "monorepo", "monorepo-plugin");
|
const monorepoPluginPath = path.join(baseDir, "external_plugins", "monorepo", "monorepo-plugin");
|
||||||
const monorepoPluginPathAbs = path.resolve(monorepoPluginPath);
|
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");
|
const notAPluginPath = path.join(baseDir, "node_modules", "not-a-plugin");
|
||||||
let pluginList;
|
let pluginList;
|
||||||
|
|
||||||
before(async function() {
|
before(async function() {
|
||||||
// Setup test plugins
|
// Setup test plugins
|
||||||
async function writePlugin(pluginPath, name) {
|
async function writePlugin(pluginPath, name, dependencies = {}, packageJsonFields = {}) {
|
||||||
await fs.mkdir(pluginPath, { recursive: true });
|
await fs.mkdir(pluginPath, { recursive: true });
|
||||||
await fs.writeFile(
|
await fs.writeFile(
|
||||||
path.join(pluginPath, "index.js"),
|
path.join(pluginPath, "index.js"),
|
||||||
@@ -141,6 +145,8 @@ describe("lib/plugin_loader", function() {
|
|||||||
name: path.basename(pluginPath),
|
name: path.basename(pluginPath),
|
||||||
version: "0.0.1",
|
version: "0.0.1",
|
||||||
keywords: ["clusterio-plugin"],
|
keywords: ["clusterio-plugin"],
|
||||||
|
dependencies,
|
||||||
|
...packageJsonFields,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -150,7 +156,13 @@ describe("lib/plugin_loader", function() {
|
|||||||
// Create external plugin
|
// Create external plugin
|
||||||
await writePlugin(externalPluginPath, "external_plugin");
|
await writePlugin(externalPluginPath, "external_plugin");
|
||||||
// Create npm 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
|
// Write a monorepo plugin
|
||||||
await writePlugin(monorepoPluginPath, "monorepo-plugin");
|
await writePlugin(monorepoPluginPath, "monorepo-plugin");
|
||||||
// Create root package.json
|
// Create root package.json
|
||||||
@@ -165,10 +177,12 @@ describe("lib/plugin_loader", function() {
|
|||||||
);
|
);
|
||||||
// Create an npm module that is not a plugin
|
// Create an npm module that is not a plugin
|
||||||
await fs.mkdir(notAPluginPath, { recursive: true });
|
await fs.mkdir(notAPluginPath, { recursive: true });
|
||||||
await fs.writeFile(
|
await fs.writeFile(path.join(notAPluginPath, "package.json"), JSON.stringify({
|
||||||
path.join(notAPluginPath, "package.json"),
|
name: "not-a-plugin",
|
||||||
JSON.stringify({ name: "not-a-plugin", version: "1.0.0" })
|
version: "1.0.0",
|
||||||
);
|
dependencies: { "hidden-plugin": "^1.0.0" },
|
||||||
|
}));
|
||||||
|
await writePlugin(hiddenPluginPath, "hidden");
|
||||||
|
|
||||||
process.chdir(baseDir);
|
process.chdir(baseDir);
|
||||||
pluginList = await lib.loadPluginList(pluginListPath, true);
|
pluginList = await lib.loadPluginList(pluginListPath, true);
|
||||||
@@ -191,6 +205,11 @@ describe("lib/plugin_loader", function() {
|
|||||||
assert.strictEqual(pluginList.get("not-a-plugin"), undefined);
|
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() {
|
it("should load existing plugin list", async function() {
|
||||||
const existingList = new Map([["test", "/test/path"]]);
|
const existingList = new Map([["test", "/test/path"]]);
|
||||||
await fs.writeFile(pluginListPath, JSON.stringify([...existingList]));
|
await fs.writeFile(pluginListPath, JSON.stringify([...existingList]));
|
||||||
|
|||||||
Reference in new issue
Block a user