Files
factorio-clusterio/packages/lib/src/data/ModuleInfo.ts
T
Hornwitser 5dcaca4d0b Migrate to ECMAScript Modules
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.
2026-09-11 22:43:57 +02:00

52 lines
1.4 KiB
TypeScript

import { Type, Static } from "@sinclair/typebox";
import { compile } from "../schema.js";
/**
* 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,
);
}
}