From 32c2f99a3124a245d02b415961bdfa789a2596ca Mon Sep 17 00:00:00 2001 From: Kallum Jones Date: Wed, 3 Aug 2022 17:47:29 +0100 Subject: [PATCH] Use spinners when installing mod --- src/mods/mods.ts | 14 ++++++++------ src/mods/sources/modrinth_source.ts | 2 +- src/util/print_utils.ts | 17 ++++++++++++----- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/mods/mods.ts b/src/mods/mods.ts index 5b70152..7fbc1be 100644 --- a/src/mods/mods.ts +++ b/src/mods/mods.ts @@ -19,16 +19,18 @@ export default class Mods { for (const source of this.MOD_SOURCES) { // If we have not yet successfully installed the queried mod if (!success) { - PrintUtils.info(`Searching for ${mod}...`); + const spinner = new PrintUtils.Spinner(`Searching for ${mod}...`); + spinner.start(); + // Search for the mod let id; try { id = await source.search(mod); } catch (e) { if (e instanceof ModNotFoundError) { - PrintUtils.info(`Mod not found on ${source.getName()}`); + spinner.updateText(`Mod not found on ${source.getName()}`) } else { - PrintUtils.error(`An error occurred searching for ${mod} on ${source.getName()}. Skipping ${source.getName()}`, e) + spinner.error(`An error occurred searching for ${mod} on ${source.getName()}. Skipping ${source.getName()}`) // Try the next source continue; } @@ -36,13 +38,13 @@ export default class Mods { // If a mod is found, install it if (id != undefined) { - PrintUtils.info(`Installing ${mod}...`); + spinner.updateText(`Installing ${mod}...`) try { await source.install(id); - PrintUtils.success(`Successfully installed ${mod}`); + spinner.succeed(`Successfully installed ${mod}`); } catch (e) { // Log the error, and continue to next source - PrintUtils.error(e); + spinner.error(e); } } } diff --git a/src/mods/sources/modrinth_source.ts b/src/mods/sources/modrinth_source.ts index d4d647e..f1d1f9d 100644 --- a/src/mods/sources/modrinth_source.ts +++ b/src/mods/sources/modrinth_source.ts @@ -66,7 +66,7 @@ export default class ModrinthSource implements ModSource { * @throws ModNotFoundError if the query returns no results. * @returns The mod id of the found mod */ - async search(query: string): Promise | never { + async search(query: string): Promise { const mcVersion = await MinecraftUtils.getCurrentMinecraftVersion(); const params = { diff --git a/src/util/print_utils.ts b/src/util/print_utils.ts index ef889d8..cabc4e5 100644 --- a/src/util/print_utils.ts +++ b/src/util/print_utils.ts @@ -23,16 +23,23 @@ export default class PrintUtils { this.spinner.stop(); } - public error() { - this.spinner.fail(); + public error(print: string | Error) { + if (print instanceof Error) { + this.spinner.fail(print.message) + if (ModManager.logger != null) { + ModManager.logger.error(print) + } + } else { + this.spinner.fail(print); + } } - public succeed() { - this.spinner.succeed(); + public succeed(print: string) { + this.spinner.succeed(print); } public updateText(text: string) { - this.spinner.info(text); + this.spinner.start(text); } public clear() {