mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-11 23:04:52 +09:00
Migrate the usage of CommonJS specific code and imports to ESM. Roughly summarised this: - Changes all require() calls to import statement or expressions. - Changes usage of module.exports to export statements. - Replaces require.resolve with import.meta.resolve. - Replaces conditional imports with conditional subpath imports. - Replaces module === require.main with import.meta.main. - Adds the full filename with extention to all relative imports. - Changes all usage of __dirname and __filename to their import.meta equivalents. - Sets "type": "module" in package.json files. - Removes usage of "use strict". - Adds .ts an an alias of .js for the webpack build. Due to differences in how require and import resolves files the plugin entrypoints will have to include the filename and extension after this migration in order to work.
92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
import { plainJson, jsonArray, JsonBoolean, JsonNumber, JsonString, StringEnum } from "@clusterio/lib";
|
|
import { Type } from "@sinclair/typebox";
|
|
|
|
export class PluginExampleEvent {
|
|
static type = "event";
|
|
static src = ["host", "control"];
|
|
static dst = ["controller", "host", "instance"];
|
|
static plugin = "__plugin_name__";
|
|
static permission = "__plugin_name__.example.permission.event";
|
|
|
|
constructor(myString, myNumberArray) {
|
|
this.myString = myString;
|
|
this.myNumberArray = myNumberArray;
|
|
}
|
|
|
|
static jsonSchema = Type.Object({
|
|
"myString": Type.String(),
|
|
"myNumberArray": Type.Array(Type.Number()),
|
|
});
|
|
|
|
static fromJSON(json) {
|
|
return new PluginExampleEvent(json.myString, json.myNumberArray);
|
|
}
|
|
}
|
|
|
|
export class PluginExampleRequest {
|
|
static type = "request";
|
|
static src = ["host", "control"];
|
|
static dst = ["controller", "host", "instance"];
|
|
static plugin = "__plugin_name__";
|
|
static permission = "__plugin_name__.example.permission.request";
|
|
|
|
constructor(myString, myNumberArray) {
|
|
this.myString = myString;
|
|
this.myNumberArray = myNumberArray;
|
|
}
|
|
|
|
static jsonSchema = Type.Object({
|
|
"myString": Type.String(),
|
|
"myNumberArray": Type.Array(Type.Number()),
|
|
});
|
|
|
|
static fromJSON(json) {
|
|
return new PluginExampleRequest(json.myString, json.myNumberArray);
|
|
}
|
|
|
|
static Response = plainJson(Type.Object({
|
|
"myResponseString": Type.String(),
|
|
"myResponseNumbers": Type.Array(Type.Number()),
|
|
}));
|
|
}
|
|
//%if controller & web // Subscribing requires web content and the controller
|
|
|
|
export class ExampleSubscribableValue {
|
|
constructor(id, updatedAtMs, isDeleted) {
|
|
this.id = id;
|
|
this.updatedAtMs = updatedAtMs;
|
|
this.isDeleted = isDeleted;
|
|
}
|
|
|
|
static jsonSchema = Type.Object({
|
|
id: Type.String(),
|
|
updatedAtMs: Type.Number(),
|
|
isDeleted: Type.Boolean(),
|
|
});
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.id, json.updatedAtMs, json.isDeleted);
|
|
}
|
|
}
|
|
|
|
export class ExampleSubscribableUpdate {
|
|
static type = "event";
|
|
static src = "controller";
|
|
static dst = "control";
|
|
static plugin = "__plugin_name__";
|
|
static permission = "__plugin_name__.example.permission.subscribe";
|
|
|
|
constructor(updates) {
|
|
this.updates = updates;
|
|
}
|
|
|
|
static jsonSchema = Type.Object({
|
|
"updates": Type.Array(ExampleSubscribableValue.jsonSchema),
|
|
});
|
|
|
|
static fromJSON(json) {
|
|
return new this(json.updates.map(update => ExampleSubscribableValue.fromJSON(update)));
|
|
}
|
|
}
|
|
//%endif
|