mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-25 01:41:05 -05:00
Added text-oriented version of metadata reader
This commit is contained in:
parent
de4393cceb
commit
a416c24813
5 changed files with 44 additions and 46 deletions
|
@ -1,19 +1,10 @@
|
||||||
import ModMetadata from "../mod-metadata";
|
import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader";
|
||||||
import ZippedModMetadataReader from "../zipped-mod-metadata-reader";
|
|
||||||
import FabricModConfig from "./fabric-mod-config";
|
import FabricModConfig from "./fabric-mod-config";
|
||||||
import FabricModMetadata from "./fabric-mod-metadata";
|
import FabricModMetadata from "./fabric-mod-metadata";
|
||||||
|
|
||||||
class FabricModMetadataReader extends ZippedModMetadataReader<FabricModConfig> {
|
class FabricModMetadataReader extends ZippedModTextMetadataReader<FabricModConfig> {
|
||||||
constructor() {
|
constructor() {
|
||||||
super(FabricModConfig.FILENAME);
|
super(FabricModConfig.FILENAME, x => new FabricModMetadata(x));
|
||||||
}
|
|
||||||
|
|
||||||
protected loadConfig(buffer: Buffer): FabricModConfig {
|
|
||||||
return JSON.parse(buffer.toString("utf8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected createMetadataFromConfig(config: FabricModConfig): ModMetadata {
|
|
||||||
return new FabricModMetadata(config);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1,11 @@
|
||||||
import ModMetadata from "../mod-metadata";
|
|
||||||
import toml from "toml";
|
import toml from "toml";
|
||||||
import ZippedModMetadataReader from "../zipped-mod-metadata-reader";
|
import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader";
|
||||||
import ForgeModMetadata from "./forge-mod-metadata";
|
import ForgeModMetadata from "./forge-mod-metadata";
|
||||||
import ForgeModConfig from "./forge-mod-config";
|
import ForgeModConfig from "./forge-mod-config";
|
||||||
|
|
||||||
class ForgeModMetadataReader extends ZippedModMetadataReader<ForgeModConfig> {
|
class ForgeModMetadataReader extends ZippedModTextMetadataReader<ForgeModConfig> {
|
||||||
constructor() {
|
constructor() {
|
||||||
super(ForgeModConfig.FILENAME);
|
super(ForgeModConfig.FILENAME, x => new ForgeModMetadata(x), toml.parse);
|
||||||
}
|
|
||||||
|
|
||||||
protected loadConfig(buffer: Buffer): ForgeModConfig {
|
|
||||||
return toml.parse(buffer.toString("utf8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected createMetadataFromConfig(config: ForgeModConfig): ModMetadata {
|
|
||||||
return new ForgeModMetadata(config);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,10 @@
|
||||||
import ModMetadata from "../mod-metadata";
|
import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader";
|
||||||
import ZippedModMetadataReader from "../zipped-mod-metadata-reader";
|
|
||||||
import QuiltModConfig from "./quilt-mod-config";
|
import QuiltModConfig from "./quilt-mod-config";
|
||||||
import QuiltModMetadata from "./quilt-mod-metadata";
|
import QuiltModMetadata from "./quilt-mod-metadata";
|
||||||
|
|
||||||
class QuiltModMetadataReader extends ZippedModMetadataReader<QuiltModConfig> {
|
class QuiltModMetadataReader extends ZippedModTextMetadataReader<QuiltModConfig> {
|
||||||
constructor() {
|
constructor() {
|
||||||
super(QuiltModConfig.FILENAME);
|
super(QuiltModConfig.FILENAME, x => new QuiltModMetadata(x));
|
||||||
}
|
|
||||||
|
|
||||||
protected loadConfig(buffer: Buffer): QuiltModConfig {
|
|
||||||
return JSON.parse(buffer.toString("utf8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected createMetadataFromConfig(config: QuiltModConfig): ModMetadata {
|
|
||||||
return new QuiltModMetadata(config);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,23 +2,19 @@ import ModMetadata from "./mod-metadata";
|
||||||
import ModMetadataReader from "./mod-metadata-reader";
|
import ModMetadataReader from "./mod-metadata-reader";
|
||||||
import { StreamZipAsync, async as ZipArchive } from "node-stream-zip";
|
import { StreamZipAsync, async as ZipArchive } from "node-stream-zip";
|
||||||
|
|
||||||
export default abstract class ZippedModMetadataReader<TConfig = Record<string, unknown>> implements ModMetadataReader {
|
abstract class ZippedModMetadataReader<TConfig = Record<string, unknown>> implements ModMetadataReader {
|
||||||
private configEntryName: string;
|
private _configEntryName: string;
|
||||||
|
|
||||||
protected constructor(configEntryName: string) {
|
protected constructor(configEntryName: string) {
|
||||||
this.configEntryName = configEntryName;
|
this._configEntryName = configEntryName;
|
||||||
}
|
}
|
||||||
|
|
||||||
async readMetadata(modPath: string): Promise<ModMetadata | null> {
|
async readMetadata(modPath: string): Promise<ModMetadata | null> {
|
||||||
let zip = <StreamZipAsync>null;
|
let zip = null as StreamZipAsync;
|
||||||
try {
|
try {
|
||||||
zip = new ZipArchive({ file: modPath });
|
zip = new ZipArchive({ file: modPath });
|
||||||
const buffer = await zip.entryData(this.configEntryName).catch(_ => <Buffer>null);
|
const buffer = await zip.entryData(this._configEntryName).catch(_ => null as Buffer);
|
||||||
if (buffer) {
|
return buffer && this.createMetadataFromConfig(this.loadConfig(buffer));
|
||||||
return this.createMetadataFromConfig(this.loadConfig(buffer));
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -30,3 +26,5 @@ export default abstract class ZippedModMetadataReader<TConfig = Record<string, u
|
||||||
|
|
||||||
protected abstract createMetadataFromConfig(config: TConfig): ModMetadata;
|
protected abstract createMetadataFromConfig(config: TConfig): ModMetadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default ZippedModMetadataReader;
|
||||||
|
|
27
src/metadata/zipped-mod-text-metadata-reader.ts
Normal file
27
src/metadata/zipped-mod-text-metadata-reader.ts
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
import ModMetadata from "./mod-metadata";
|
||||||
|
import ZippedModMetadataReader from "./zipped-mod-metadata-reader";
|
||||||
|
|
||||||
|
type ModMetadataFactory<T> = (config: T) => ModMetadata;
|
||||||
|
|
||||||
|
type Parser<T> = (input: string) => T;
|
||||||
|
|
||||||
|
abstract class ZippedModTextMetadataReader<TConfig = Record<string, unknown>> extends ZippedModMetadataReader<TConfig> {
|
||||||
|
private readonly _factory: ModMetadataFactory<TConfig>;
|
||||||
|
private readonly _parser: Parser<TConfig>;
|
||||||
|
|
||||||
|
protected constructor(configEntryName: string, factory: ModMetadataFactory<TConfig>, parser?: Parser<any>) {
|
||||||
|
super(configEntryName);
|
||||||
|
this._factory = factory;
|
||||||
|
this._parser = parser ?? JSON.parse;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected loadConfig(buffer: Buffer): TConfig {
|
||||||
|
return this._parser(buffer.toString("utf8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected createMetadataFromConfig(config: TConfig): ModMetadata {
|
||||||
|
return this._factory(config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default ZippedModTextMetadataReader;
|
Loading…
Reference in a new issue