mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-15 15:24:52 +09:00
Add packagePath to plugin info
Provide the absolute path to the package.json file in the resolved plugin info to simplify the logic around loading files from it.
This commit is contained in:
1 parent
d6dce12acc
commit
30083b96c1
8 files changed
+20
-18
No files matched your search
@@ -644,8 +644,7 @@ export default class Controller {
|
||||
}
|
||||
|
||||
packageName = pluginInfo.npmPackage ?? pluginInfo.name;
|
||||
const pluginPackageJsonPath = require.resolve(path.posix.join(pluginInfo.requirePath, "package.json"));
|
||||
const pluginPackageJson = JSON.parse(await fs.readFile(pluginPackageJsonPath, "utf8"));
|
||||
const pluginPackageJson = JSON.parse(await fs.readFile(pluginInfo.packagePath, "utf8"));
|
||||
if (pluginInfo.version !== pluginPackageJson.version) {
|
||||
this.config.restartRequired = true;
|
||||
return true;
|
||||
|
||||
@@ -876,8 +876,7 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
|
||||
packageName = pluginInfo.npmPackage ?? pluginInfo.name;
|
||||
const pluginPackageJsonPath = require.resolve(path.posix.join(pluginInfo.requirePath, "package.json"));
|
||||
const pluginPackageJson = JSON.parse(await fs.readFile(pluginPackageJsonPath, "utf8"));
|
||||
const pluginPackageJson = JSON.parse(await fs.readFile(pluginInfo.packagePath, "utf8"));
|
||||
if (pluginInfo.version !== pluginPackageJson.version) {
|
||||
this.config.restartRequired = true;
|
||||
return true;
|
||||
|
||||
@@ -91,8 +91,7 @@ export class SaveModule {
|
||||
}
|
||||
|
||||
static async fromPlugin(plugin: BaseInstancePlugin) {
|
||||
let pluginPackagePath = require.resolve(path.posix.join(plugin.info.requirePath, "package.json"));
|
||||
let moduleDirectory = path.join(path.dirname(pluginPackagePath), "module");
|
||||
let moduleDirectory = path.join(path.dirname(plugin.info.packagePath), "module");
|
||||
try {
|
||||
await fs.access(moduleDirectory);
|
||||
} catch (err: any) {
|
||||
|
||||
@@ -46,6 +46,10 @@ export type PluginNodeEnvInfo = PluginDeclaration & {
|
||||
* server in order for the web interface to be able to load the plugin.
|
||||
*/
|
||||
webStaticPath: string;
|
||||
/**
|
||||
* Absolute path to the package.json file for the plugin.
|
||||
*/
|
||||
packagePath: string;
|
||||
requirePath: string;
|
||||
version: string;
|
||||
manifest: any;
|
||||
|
||||
@@ -26,11 +26,15 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
for (let [pluginName, pluginPath] of pluginList) {
|
||||
let pluginInfo: libPlugin.PluginNodeEnvInfo;
|
||||
let pluginPackage: { name?: string, version: string, main?: string, private?: boolean };
|
||||
let packagePath;
|
||||
|
||||
// Check if plugin path exists, otherwise remove it
|
||||
// Check if plugin has a package.json file, otherwise remove it
|
||||
try {
|
||||
require.resolve(pluginPath);
|
||||
} catch {
|
||||
const absolute = path.isAbsolute(pluginPath);
|
||||
const packageImport = path.posix.join(pluginPath, "package.json");
|
||||
packagePath = require.resolve(packageImport);
|
||||
await fs.access(packagePath, fs.constants.F_OK);
|
||||
} catch (err) {
|
||||
let errMsg = `Plugin path ${pluginPath} does not exist`;
|
||||
try {
|
||||
await fs.access(pluginPath, fs.constants.F_OK);
|
||||
@@ -43,7 +47,7 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
|
||||
try {
|
||||
pluginInfo = require(pluginPath).plugin;
|
||||
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
|
||||
pluginPackage = require(packagePath);
|
||||
} catch (err: any) {
|
||||
if (err.code === "InstallationError") {
|
||||
throw err;
|
||||
@@ -69,12 +73,8 @@ export async function loadPluginInfos(pluginList: Map<string, string>) {
|
||||
continue;
|
||||
}
|
||||
|
||||
pluginInfo.webStaticPath = path.join(
|
||||
path.dirname(
|
||||
require.resolve(path.posix.join(pluginPath, "package.json"))
|
||||
),
|
||||
"dist", "web", "static",
|
||||
);
|
||||
pluginInfo.webStaticPath = path.join(path.dirname(packagePath), "dist", "web", "static");
|
||||
pluginInfo.packagePath = packagePath;
|
||||
pluginInfo.requirePath = pluginPath;
|
||||
pluginInfo.version = pluginPackage.version;
|
||||
pluginInfo.npmPackage = !pluginPackage.private && pluginPath === pluginPackage.name ? pluginPath : undefined;
|
||||
|
||||
@@ -241,7 +241,7 @@ describe("controller/src/Controller", function() {
|
||||
controller.config.set("global_chat.load_plugin", true);
|
||||
controller.pluginInfos[0] = {
|
||||
name: "global_chat",
|
||||
requirePath: path.dirname(require.resolve("@clusterio/controller/package.json")),
|
||||
packagePath: require.resolve("@clusterio/controller/package.json"),
|
||||
version: controllerVersion,
|
||||
};
|
||||
});
|
||||
|
||||
+1
-1
@@ -123,7 +123,7 @@ describe("Host testing", function() {
|
||||
// This can be used more generally, but i did not want to interfere with handleSyncUserListsEvent
|
||||
const pluginInfos = [{
|
||||
name: "global_chat",
|
||||
requirePath: path.dirname(require.resolve("@clusterio/host/package.json")),
|
||||
packagePath: require.resolve("@clusterio/host/package.json"),
|
||||
version: hostVersion,
|
||||
}];
|
||||
const hostConfig = new lib.HostConfig("host", { "host.version": hostVersion });
|
||||
|
||||
@@ -47,6 +47,7 @@ describe("lib/plugin_loader", function() {
|
||||
name: "test",
|
||||
version: "0.0.1",
|
||||
npmPackage: undefined,
|
||||
packagePath: path.resolve(path.join(testPlugin, "package.json")),
|
||||
requirePath: path.resolve(testPlugin),
|
||||
webStaticPath: path.resolve(path.join(testPlugin, "dist", "web", "static")),
|
||||
}]
|
||||
|
||||
Reference in new issue
Block a user