Made interface for uploaders

This commit is contained in:
Kir_Antipov 2023-05-04 10:42:32 +00:00
parent 1a9e1f14d7
commit 03239ebbb7
3 changed files with 51 additions and 46 deletions

View file

@ -0,0 +1,51 @@
import { CurseForgeUploader } from "./curseforge/curseforge-uploader";
import { GenericPlatformUploadRequest, KnownPlatformUploaderOptions } from "./generic-platform-uploader";
import { GitHubUploader } from "./github/github-uploader";
import { ModrinthUploader } from "./modrinth/modrinth-uploader";
import { PlatformType } from "./platform-type";
/**
* Represents a platform uploader.
*
* @template TRequest - The type of content that can be uploaded using the uploader.
* @template TReport - The type of report that is returned after the upload process.
*/
export interface PlatformUploader<TRequest, TReport> {
/**
* Returns the type of platform that the uploader supports.
*/
get platform(): PlatformType;
/**
* Processes the provided upload request.
*
* @param request - The upload request to process.
*
* @returns A report generated after the upload.
*/
upload(request: TRequest): Promise<TReport>;
}
/**
* Creates a new platform uploader based on the provided platform type and options.
*
* @param platform - The type of platform for which to create the uploader.
* @param options - The options to configure the uploader.
*
* @returns A new platform uploader.
*/
export function createPlatformUploader(platform: PlatformType, options: KnownPlatformUploaderOptions): PlatformUploader<GenericPlatformUploadRequest, unknown> {
switch (platform) {
case PlatformType.MODRINTH:
return new ModrinthUploader(options);
case PlatformType.CURSEFORGE:
return new CurseForgeUploader(options);
case PlatformType.GITHUB:
return new GitHubUploader(options);
default:
throw new Error(`Unknown platform "${PlatformType.format(platform)}."`);
}
}

View file

@ -1,24 +0,0 @@
import Publisher from "./publisher";
import PublisherTarget from "./publisher-target";
import GitHubPublisher from "./github/github-publisher";
import ModrinthPublisher from "./modrinth/modrinth-publisher";
import CurseForgePublisher from "./curseforge/curseforge-publisher";
import Logger from "../utils/logging/logger";
export default class PublisherFactory {
public create(target: PublisherTarget, logger?: Logger): Publisher<unknown> {
switch(target) {
case PublisherTarget.GitHub:
return new GitHubPublisher(logger);
case PublisherTarget.Modrinth:
return new ModrinthPublisher(logger);
case PublisherTarget.CurseForge:
return new CurseForgePublisher(logger);
default:
throw new Error(`Unknown target "${PublisherTarget.toString(target)}"`);
}
}
}

View file

@ -1,22 +0,0 @@
import File from "../utils/io/file";
import Logger from "../utils/logging/logger";
import { getEmptyLogger } from "../utils/logging/logger";
import PublisherTarget from "./publisher-target";
export default abstract class Publisher<TOptions> {
protected readonly logger: Logger;
public constructor(logger?: Logger) {
this.logger = logger || getEmptyLogger();
}
public abstract get target(): PublisherTarget;
public abstract publish(files: File[], options: TOptions): Promise<void>;
protected validateOptions(options: TOptions): void | never {
if (!options || typeof options !== "object") {
throw new Error(`Expected options to be an object, got ${options ? typeof options : options}`);
}
}
}