mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-12-28 01:24:53 -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 ZippedModMetadataReader from "../zipped-mod-metadata-reader";
|
||||
import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader";
|
||||
import FabricModConfig from "./fabric-mod-config";
|
||||
import FabricModMetadata from "./fabric-mod-metadata";
|
||||
|
||||
class FabricModMetadataReader extends ZippedModMetadataReader<FabricModConfig> {
|
||||
class FabricModMetadataReader extends ZippedModTextMetadataReader<FabricModConfig> {
|
||||
constructor() {
|
||||
super(FabricModConfig.FILENAME);
|
||||
}
|
||||
|
||||
protected loadConfig(buffer: Buffer): FabricModConfig {
|
||||
return JSON.parse(buffer.toString("utf8"));
|
||||
}
|
||||
|
||||
protected createMetadataFromConfig(config: FabricModConfig): ModMetadata {
|
||||
return new FabricModMetadata(config);
|
||||
super(FabricModConfig.FILENAME, x => new FabricModMetadata(x));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +1,11 @@
|
|||
import ModMetadata from "../mod-metadata";
|
||||
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 ForgeModConfig from "./forge-mod-config";
|
||||
|
||||
class ForgeModMetadataReader extends ZippedModMetadataReader<ForgeModConfig> {
|
||||
class ForgeModMetadataReader extends ZippedModTextMetadataReader<ForgeModConfig> {
|
||||
constructor() {
|
||||
super(ForgeModConfig.FILENAME);
|
||||
}
|
||||
|
||||
protected loadConfig(buffer: Buffer): ForgeModConfig {
|
||||
return toml.parse(buffer.toString("utf8"));
|
||||
}
|
||||
|
||||
protected createMetadataFromConfig(config: ForgeModConfig): ModMetadata {
|
||||
return new ForgeModMetadata(config);
|
||||
super(ForgeModConfig.FILENAME, x => new ForgeModMetadata(x), toml.parse);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,19 +1,10 @@
|
|||
import ModMetadata from "../mod-metadata";
|
||||
import ZippedModMetadataReader from "../zipped-mod-metadata-reader";
|
||||
import ZippedModTextMetadataReader from "../zipped-mod-text-metadata-reader";
|
||||
import QuiltModConfig from "./quilt-mod-config";
|
||||
import QuiltModMetadata from "./quilt-mod-metadata";
|
||||
|
||||
class QuiltModMetadataReader extends ZippedModMetadataReader<QuiltModConfig> {
|
||||
class QuiltModMetadataReader extends ZippedModTextMetadataReader<QuiltModConfig> {
|
||||
constructor() {
|
||||
super(QuiltModConfig.FILENAME);
|
||||
}
|
||||
|
||||
protected loadConfig(buffer: Buffer): QuiltModConfig {
|
||||
return JSON.parse(buffer.toString("utf8"));
|
||||
}
|
||||
|
||||
protected createMetadataFromConfig(config: QuiltModConfig): ModMetadata {
|
||||
return new QuiltModMetadata(config);
|
||||
super(QuiltModConfig.FILENAME, x => new QuiltModMetadata(x));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,23 +2,19 @@ import ModMetadata from "./mod-metadata";
|
|||
import ModMetadataReader from "./mod-metadata-reader";
|
||||
import { StreamZipAsync, async as ZipArchive } from "node-stream-zip";
|
||||
|
||||
export default abstract class ZippedModMetadataReader<TConfig = Record<string, unknown>> implements ModMetadataReader {
|
||||
private configEntryName: string;
|
||||
abstract class ZippedModMetadataReader<TConfig = Record<string, unknown>> implements ModMetadataReader {
|
||||
private _configEntryName: string;
|
||||
|
||||
protected constructor(configEntryName: string) {
|
||||
this.configEntryName = configEntryName;
|
||||
this._configEntryName = configEntryName;
|
||||
}
|
||||
|
||||
async readMetadata(modPath: string): Promise<ModMetadata | null> {
|
||||
let zip = <StreamZipAsync>null;
|
||||
let zip = null as StreamZipAsync;
|
||||
try {
|
||||
zip = new ZipArchive({ file: modPath });
|
||||
const buffer = await zip.entryData(this.configEntryName).catch(_ => <Buffer>null);
|
||||
if (buffer) {
|
||||
return this.createMetadataFromConfig(this.loadConfig(buffer));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
const buffer = await zip.entryData(this._configEntryName).catch(_ => null as Buffer);
|
||||
return buffer && this.createMetadataFromConfig(this.loadConfig(buffer));
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
|
@ -30,3 +26,5 @@ export default abstract class ZippedModMetadataReader<TConfig = Record<string, u
|
|||
|
||||
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