diff --git a/src/loaders/fabric/fabric-metadata-reader.ts b/src/loaders/fabric/fabric-metadata-reader.ts new file mode 100644 index 0000000..36011dc --- /dev/null +++ b/src/loaders/fabric/fabric-metadata-reader.ts @@ -0,0 +1,15 @@ +import { ZippedTextLoaderMetadataReader } from "@/loaders/zipped-loader-metadata-reader"; +import { FabricMetadata } from "./fabric-metadata"; +import { FABRIC_MOD_JSON, RawFabricMetadata } from "./raw-fabric-metadata"; + +/** + * A metadata reader that is able to read Fabric mod metadata from a zipped file. + */ +export class FabricMetadataReader extends ZippedTextLoaderMetadataReader { + /** + * Constructs a new {@link FabricMetadataReader} instance. + */ + constructor() { + super(FABRIC_MOD_JSON, FabricMetadata.from, JSON.parse); + } +} diff --git a/tests/unit/loaders/fabric/fabric-metadata-reader.spec.ts b/tests/unit/loaders/fabric/fabric-metadata-reader.spec.ts new file mode 100644 index 0000000..cda508a --- /dev/null +++ b/tests/unit/loaders/fabric/fabric-metadata-reader.spec.ts @@ -0,0 +1,41 @@ +import { zipFile } from "@/../tests/utils/zip-utils"; +import mockFs from "mock-fs"; +import { FabricMetadata } from "@/loaders/fabric/fabric-metadata"; +import { FabricMetadataReader } from "@/loaders/fabric/fabric-metadata-reader"; + +beforeEach(async () => { + mockFs({ + "fabric.mod.jar": await zipFile([__dirname, "../../../content/fabric/fabric.mod.json"]), + "text.txt": "", + }); +}); + +afterEach(() => { + mockFs.restore(); +}); + +describe("FabricMetadataReader", () => { + test("successfully reads fabric.mod.json", async () => { + const reader = new FabricMetadataReader(); + + const metadata = await reader.readMetadataFile("fabric.mod.jar"); + + expect(metadata).toBeInstanceOf(FabricMetadata); + }); + + test("returns undefined if file is not a Fabric mod", async () => { + const reader = new FabricMetadataReader(); + + const metadata = await reader.readMetadataFile("text.txt"); + + expect(metadata).toBeUndefined(); + }); + + test("returns undefined if file does not exist", async () => { + const reader = new FabricMetadataReader(); + + const metadata = await reader.readMetadataFile("text.json"); + + expect(metadata).toBeUndefined(); + }); +});