Use spinners when installing mod

This commit is contained in:
Kallum Jones 2022-08-03 17:47:29 +01:00
parent 2b5d2a3643
commit 32c2f99a31
No known key found for this signature in database
GPG Key ID: D7F4589C4D7F81A9
3 changed files with 21 additions and 12 deletions

View File

@ -19,16 +19,18 @@ export default class Mods {
for (const source of this.MOD_SOURCES) { for (const source of this.MOD_SOURCES) {
// If we have not yet successfully installed the queried mod // If we have not yet successfully installed the queried mod
if (!success) { if (!success) {
PrintUtils.info(`Searching for ${mod}...`); const spinner = new PrintUtils.Spinner(`Searching for ${mod}...`);
spinner.start();
// Search for the mod // Search for the mod
let id; let id;
try { try {
id = await source.search(mod); id = await source.search(mod);
} catch (e) { } catch (e) {
if (e instanceof ModNotFoundError) { if (e instanceof ModNotFoundError) {
PrintUtils.info(`Mod not found on ${source.getName()}`); spinner.updateText(`Mod not found on ${source.getName()}`)
} else { } 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 // Try the next source
continue; continue;
} }
@ -36,13 +38,13 @@ export default class Mods {
// If a mod is found, install it // If a mod is found, install it
if (id != undefined) { if (id != undefined) {
PrintUtils.info(`Installing ${mod}...`); spinner.updateText(`Installing ${mod}...`)
try { try {
await source.install(id); await source.install(id);
PrintUtils.success(`Successfully installed ${mod}`); spinner.succeed(`Successfully installed ${mod}`);
} catch (e) { } catch (e) {
// Log the error, and continue to next source // Log the error, and continue to next source
PrintUtils.error(e); spinner.error(e);
} }
} }
} }

View File

@ -66,7 +66,7 @@ export default class ModrinthSource implements ModSource {
* @throws ModNotFoundError if the query returns no results. * @throws ModNotFoundError if the query returns no results.
* @returns The mod id of the found mod * @returns The mod id of the found mod
*/ */
async search(query: string): Promise<string> | never { async search(query: string): Promise<string> {
const mcVersion = await MinecraftUtils.getCurrentMinecraftVersion(); const mcVersion = await MinecraftUtils.getCurrentMinecraftVersion();
const params = { const params = {

View File

@ -23,16 +23,23 @@ export default class PrintUtils {
this.spinner.stop(); this.spinner.stop();
} }
public error() { public error(print: string | Error) {
this.spinner.fail(); if (print instanceof Error) {
this.spinner.fail(print.message)
if (ModManager.logger != null) {
ModManager.logger.error(print)
}
} else {
this.spinner.fail(print);
}
} }
public succeed() { public succeed(print: string) {
this.spinner.succeed(); this.spinner.succeed(print);
} }
public updateText(text: string) { public updateText(text: string) {
this.spinner.info(text); this.spinner.start(text);
} }
public clear() { public clear() {