Fix plugin version not being loaded

Load the version of the plugin from its package.json file and document
this requirement in the writing plugins document.
This commit is contained in:
Hornwitser committed 2020-10-23 16:57:49 +02:00
1 parent c5a14db8ea
commit 1b0b245484
3 files changed
+23 -9

No files matched your search

+15 -8
View File
@@ -39,8 +39,21 @@ The basic file structure of a plugin is the following.
+- module.json +- module.json
+- plugin.lua +- plugin.lua
The most important file is the `info.js` file. Without it the plugin Clusterio plugins are Node.js packages that can be published on npm for
will not recognized by Clusterio. Here's an example of it: ease of installation and distribution. The usual guides for creating
such packages apply. At minimum the `package.json` file must contain a
version entry.
A possible workflow for developing plugins is to place the plugin in a
sub-directory of where clusterio has been installed, and rely on Node.js
searching up the folder heirarchy for it to find `@clusterio/lib`. To
add it to `plugin-list.json` so that it gets loaded use the `plugin add
<path>` sub-command to either clusteriomaster, clusterioslave or
clusterioctl. Note that it's important that the path starts with ./ or
../ (use .\ or ..\ on Windows).
For a plugin the most important file is the `info.js` file. Without it
the plugin will not recognized by Clusterio. Here's an example of it:
const link = require('@clusterio/lib/link'); // For messages const link = require('@clusterio/lib/link'); // For messages
@@ -48,7 +61,6 @@ will not recognized by Clusterio. Here's an example of it:
name: "foo_frobber", name: "foo_frobber",
title: "Foo Frobber", title: "Foo Frobber",
description: "Does advanced frobnication", description: "Does advanced frobnication",
version: "0.8.1",
instanceEntrypoint: "instance", instanceEntrypoint: "instance",
masterEntrypoint: "master", masterEntrypoint: "master",
messages: { messages: {
@@ -68,11 +80,6 @@ The following properties are recognized:
**description**: **description**:
Brief description of what the plugin does. Currently not used. Brief description of what the plugin does. Currently not used.
**version**:
Semver compatible version string of the plugin. Currently only
servers as the default for the Clusterio module embedded in the
plugin, if any.
**instanceEntrypoint**: **instanceEntrypoint**:
Path to a Node.js module relative to the plugin directory which Path to a Node.js module relative to the plugin directory which
contains the InstancePlugin class definition for this plugin. This contains the InstancePlugin class definition for this plugin. This
+3
View File
@@ -402,9 +402,11 @@ async function loadPluginInfos(pluginList) {
let plugins = []; let plugins = [];
for (let [pluginName, pluginPath] of pluginList) { for (let [pluginName, pluginPath] of pluginList) {
let pluginInfo; let pluginInfo;
let pluginPackage;
try { try {
pluginInfo = require(path.posix.join(pluginPath, "info")); pluginInfo = require(path.posix.join(pluginPath, "info"));
pluginPackage = require(path.posix.join(pluginPath, "package.json"));
} catch (err) { } catch (err) {
throw new errors.PluginError(pluginName, err); throw new errors.PluginError(pluginName, err);
@@ -417,6 +419,7 @@ async function loadPluginInfos(pluginList) {
} }
pluginInfo.requirePath = pluginPath; pluginInfo.requirePath = pluginPath;
pluginInfo.version = pluginPackage.version;
plugins.push(pluginInfo); plugins.push(pluginInfo);
} }
return plugins; return plugins;
+5 -1
View File
@@ -54,6 +54,10 @@ describe("lib/plugin", function() {
path.join(pluginPath, "info.js"), path.join(pluginPath, "info.js"),
`module.exports = { name: "${infoName}" };` `module.exports = { name: "${infoName}" };`
); );
await fs.outputFile(
path.join(pluginPath, "package.json"),
'{ "version": "0.0.1" }'
);
} }
await writePlugin(testPlugin, "test"); await writePlugin(testPlugin, "test");
@@ -71,7 +75,7 @@ describe("lib/plugin", function() {
it("should load test plugin", async function() { it("should load test plugin", async function() {
assert.deepEqual( assert.deepEqual(
await plugin.loadPluginInfos(new Map([["test", path.resolve(testPlugin)]])), await plugin.loadPluginInfos(new Map([["test", path.resolve(testPlugin)]])),
[{ name: "test", requirePath: path.resolve(testPlugin) }] [{ name: "test", version: "0.0.1", requirePath: path.resolve(testPlugin) }]
); );
}); });
it("should reject on broken plugin", async function() { it("should reject on broken plugin", async function() {