From 03239ebbb76f68ae8e139a990ffeb4774f65ea84 Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Thu, 4 May 2023 10:42:32 +0000 Subject: [PATCH] Made interface for uploaders --- src/platforms/platform-uploader.ts | 51 +++++++++++++++++++++++++++++ src/publishing/publisher-factory.ts | 24 -------------- src/publishing/publisher.ts | 22 ------------- 3 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 src/platforms/platform-uploader.ts delete mode 100644 src/publishing/publisher-factory.ts delete mode 100644 src/publishing/publisher.ts diff --git a/src/platforms/platform-uploader.ts b/src/platforms/platform-uploader.ts new file mode 100644 index 0000000..d4cbd8d --- /dev/null +++ b/src/platforms/platform-uploader.ts @@ -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 { + /** + * 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; +} + +/** + * 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 { + 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)}."`); + } +} diff --git a/src/publishing/publisher-factory.ts b/src/publishing/publisher-factory.ts deleted file mode 100644 index e36047d..0000000 --- a/src/publishing/publisher-factory.ts +++ /dev/null @@ -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 { - 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)}"`); - } - } -} diff --git a/src/publishing/publisher.ts b/src/publishing/publisher.ts deleted file mode 100644 index a1fb620..0000000 --- a/src/publishing/publisher.ts +++ /dev/null @@ -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 { - 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; - - 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}`); - } - } -}