From 1b0b2454841a7aa12bfe58f638783514cfa2ce41 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 23 Oct 2020 16:57:49 +0200 Subject: [PATCH] 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. --- docs/writing-plugins.md | 23 +++++++++++++++-------- packages/lib/plugin.js | 3 +++ test/lib/plugin.js | 6 +++++- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/docs/writing-plugins.md b/docs/writing-plugins.md index 60f6ecec..680b53a7 100644 --- a/docs/writing-plugins.md +++ b/docs/writing-plugins.md @@ -39,8 +39,21 @@ The basic file structure of a plugin is the following. +- module.json +- plugin.lua -The most important file is the `info.js` file. Without it the plugin -will not recognized by Clusterio. Here's an example of it: +Clusterio plugins are Node.js packages that can be published on npm for +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 +` 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 @@ -48,7 +61,6 @@ will not recognized by Clusterio. Here's an example of it: name: "foo_frobber", title: "Foo Frobber", description: "Does advanced frobnication", - version: "0.8.1", instanceEntrypoint: "instance", masterEntrypoint: "master", messages: { @@ -68,11 +80,6 @@ The following properties are recognized: **description**: 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**: Path to a Node.js module relative to the plugin directory which contains the InstancePlugin class definition for this plugin. This diff --git a/packages/lib/plugin.js b/packages/lib/plugin.js index 2bd00b00..f7ccbb6c 100644 --- a/packages/lib/plugin.js +++ b/packages/lib/plugin.js @@ -402,9 +402,11 @@ async function loadPluginInfos(pluginList) { let plugins = []; for (let [pluginName, pluginPath] of pluginList) { let pluginInfo; + let pluginPackage; try { pluginInfo = require(path.posix.join(pluginPath, "info")); + pluginPackage = require(path.posix.join(pluginPath, "package.json")); } catch (err) { throw new errors.PluginError(pluginName, err); @@ -417,6 +419,7 @@ async function loadPluginInfos(pluginList) { } pluginInfo.requirePath = pluginPath; + pluginInfo.version = pluginPackage.version; plugins.push(pluginInfo); } return plugins; diff --git a/test/lib/plugin.js b/test/lib/plugin.js index 445ab317..2c1e7602 100644 --- a/test/lib/plugin.js +++ b/test/lib/plugin.js @@ -54,6 +54,10 @@ describe("lib/plugin", function() { path.join(pluginPath, "info.js"), `module.exports = { name: "${infoName}" };` ); + await fs.outputFile( + path.join(pluginPath, "package.json"), + '{ "version": "0.0.1" }' + ); } await writePlugin(testPlugin, "test"); @@ -71,7 +75,7 @@ describe("lib/plugin", function() { it("should load test plugin", async function() { assert.deepEqual( 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() {