Refactor module patching logic

Break out the module info and patch info into data classes and pack all
the files for a module into one more easily modified structure.
This commit is contained in:
Hornwitser committed 2023-08-25 16:33:02 +02:00
1 parent b1a6628bfa
commit a1148c7ec9
6 files changed
+339 -206

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
import { Type, Static } from "@sinclair/typebox";
import { compile } from "../schema";
/**
* Information about a module stored in its module.json file
*/
export default class ModuleInfo {
constructor(
/** Name of the module */
public name: string,
/** Version of this module */
public version: string,
/** Paths into the module that should be loaded into the event handler */
public load: string[] = [],
/** Paths into the module that should be required by control.lua */
public require: string[] = [],
/** Dependencies of this module */
public dependencies = new Map<string, string>(),
) { }
static jsonSchema = Type.Object({
"name": Type.String(),
"version": Type.String(),
"dependencies": Type.Optional(Type.Record(Type.String(), Type.String())),
"files": Type.Optional(Type.Array(Type.String())),
"require": Type.Optional(Type.Array(Type.String())),
"load": Type.Optional(Type.Array(Type.String())),
});
static validate = compile<Static<typeof this.jsonSchema>>(this.jsonSchema as any);
toJSON() {
return {
name: this.name,
version: this.version,
dependencies: Object.fromEntries(this.dependencies),
require: this.require,
load: this.load,
};
}
static fromJSON(json: Static<typeof ModuleInfo.jsonSchema>) {
return new this(
json.name,
json.version,
json.load,
json.require,
json.dependencies ? new Map(Object.entries(json.dependencies)) : undefined,
);
}
}