make hashformat a string enum
This commit is contained in:
parent
f4e20ef240
commit
6b6a5f3cfb
3 changed files with 52 additions and 21 deletions
15
src/hashFormat.ts
Normal file
15
src/hashFormat.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
export enum HashFormat {
|
||||
"sha256" = "sha256",
|
||||
"sha512" = "sha512",
|
||||
"sha1" = "sha1",
|
||||
"md5" = "md5",
|
||||
"murmur2" = "murmur2",
|
||||
}
|
||||
|
||||
export function isValidHashFormat(hashFormat: string): HashFormat {
|
||||
if (hashFormat in HashFormat) {
|
||||
return HashFormat[hashFormat as keyof typeof HashFormat];
|
||||
} else {
|
||||
throw new Error("Invalid index file hash format!");
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import {
|
|||
CurseForgeProvider,
|
||||
GitHubProvider,
|
||||
} from "./provider";
|
||||
import { HashFormat, isValidHashFormat } from "./hashFormat";
|
||||
|
||||
export interface Metafile {
|
||||
name: string;
|
||||
|
@ -25,18 +26,20 @@ export interface Option {
|
|||
* Represents a file entry in the Packwiz index.
|
||||
*/
|
||||
export class IndexFileEntry {
|
||||
readonly file: Resource;
|
||||
readonly hash: string;
|
||||
readonly metafile: boolean;
|
||||
|
||||
constructor(
|
||||
file: string | Resource,
|
||||
hash: string,
|
||||
metafile: boolean = false,
|
||||
readonly file: Resource,
|
||||
readonly hash: string,
|
||||
readonly hashFormat: HashFormat,
|
||||
readonly alias?: string,
|
||||
readonly metafile: boolean = false,
|
||||
readonly preserve: boolean = false,
|
||||
) {
|
||||
this.file = file instanceof Resource ? file : new Resource(file);
|
||||
this.hash = hash;
|
||||
this.hashFormat = isValidHashFormat(hashFormat);
|
||||
this.alias = alias;
|
||||
this.metafile = metafile;
|
||||
this.preserve = preserve;
|
||||
}
|
||||
|
||||
async parse(): Promise<Metafile> {
|
||||
|
@ -67,7 +70,7 @@ export class IndexFileEntry {
|
|||
if (parsed.update?.modrinth) {
|
||||
return new ModrinthProvider(
|
||||
parsed.download.hash,
|
||||
parsed.download["hash-format"],
|
||||
isValidHashFormat(parsed.download["hash-format"]),
|
||||
new Resource(parsed.download.url),
|
||||
parsed.update.modrinth["mod-id"],
|
||||
parsed.update.modrinth["version"],
|
||||
|
@ -75,7 +78,7 @@ export class IndexFileEntry {
|
|||
} else if (parsed.update?.curseforge) {
|
||||
return new CurseForgeProvider(
|
||||
parsed.download.hash,
|
||||
parsed.download["hash-format"],
|
||||
isValidHashFormat(parsed.download["hash-format"]),
|
||||
parsed.download.mode,
|
||||
parsed.update.curseforge["file-id"],
|
||||
parsed.update.curseforge["project-id"],
|
||||
|
@ -83,7 +86,7 @@ export class IndexFileEntry {
|
|||
} else if (parsed.update?.github) {
|
||||
return new GitHubProvider(
|
||||
parsed.download.hash,
|
||||
parsed.download["hash-format"],
|
||||
isValidHashFormat(parsed.download["hash-format"]),
|
||||
new Resource(parsed.download.url),
|
||||
parsed.update.github.branch,
|
||||
parsed.update.github.regex,
|
||||
|
@ -93,7 +96,7 @@ export class IndexFileEntry {
|
|||
} else if (parsed.download) {
|
||||
return new UrlProvider(
|
||||
parsed.download.hash,
|
||||
parsed.download["hash-format"],
|
||||
isValidHashFormat(parsed.download["hash-format"]),
|
||||
new Resource(parsed.download.url),
|
||||
);
|
||||
} else {
|
||||
|
@ -107,7 +110,7 @@ export class IndexFileEntry {
|
|||
*/
|
||||
export interface PackwizIndex {
|
||||
location: Resource;
|
||||
hashFormat: string;
|
||||
hashFormat: HashFormat;
|
||||
files: IndexFileEntry[];
|
||||
}
|
||||
|
||||
|
@ -125,16 +128,28 @@ export async function parsePackwizIndex(
|
|||
): Promise<PackwizIndex> {
|
||||
const indexFile = new Resource(indexFilePath);
|
||||
const parsed = toml.parse(await indexFile.fetchContents());
|
||||
const hashFormat = isValidHashFormat(parsed["hash-format"]);
|
||||
return {
|
||||
location: indexFile,
|
||||
hashFormat: parsed["hash-format"],
|
||||
hashFormat: hashFormat,
|
||||
files: parsed.files.map(
|
||||
(file: any) =>
|
||||
new IndexFileEntry(
|
||||
(file: {
|
||||
file: string;
|
||||
hash: string;
|
||||
hashFormat: HashFormats;
|
||||
alias?: string;
|
||||
metafile?: boolean;
|
||||
preserve?: boolean;
|
||||
}) => {
|
||||
return new IndexFileEntry(
|
||||
indexFile.parent.join(file.file),
|
||||
file.hash,
|
||||
file.metafile,
|
||||
),
|
||||
file.hashFormat || hashFormat,
|
||||
file.alias,
|
||||
file.metafile ?? false,
|
||||
file.preserve ?? false,
|
||||
);
|
||||
},
|
||||
),
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import Resource from "./resource";
|
||||
import { HashFormat } from "./hashFormat";
|
||||
|
||||
export class UrlProvider {
|
||||
constructor(
|
||||
public hash: string,
|
||||
public hashFormat: string,
|
||||
public hashFormat: HashFormat,
|
||||
public url: Resource,
|
||||
) {}
|
||||
}
|
||||
|
@ -11,7 +12,7 @@ export class UrlProvider {
|
|||
export class ModrinthProvider extends UrlProvider {
|
||||
constructor(
|
||||
hash: string,
|
||||
hashFormat: string,
|
||||
hashFormat: HashFormat,
|
||||
url: Resource,
|
||||
public modId: string,
|
||||
public versionId: string,
|
||||
|
@ -23,7 +24,7 @@ export class ModrinthProvider extends UrlProvider {
|
|||
export class GitHubProvider extends UrlProvider {
|
||||
constructor(
|
||||
hash: string,
|
||||
hashFormat: string,
|
||||
hashFormat: HashFormat,
|
||||
url: Resource,
|
||||
public branch: string,
|
||||
public regex: string,
|
||||
|
@ -37,7 +38,7 @@ export class GitHubProvider extends UrlProvider {
|
|||
export class CurseForgeProvider extends UrlProvider {
|
||||
constructor(
|
||||
hash: string,
|
||||
hashFormat: string,
|
||||
hashFormat: HashFormat,
|
||||
public mode: string,
|
||||
public fileId: number,
|
||||
public projectId: number,
|
||||
|
|
Loading…
Add table
Reference in a new issue