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