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) {
// 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);
}
}
}

View File

@ -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<string> | never {
async search(query: string): Promise<string> {
const mcVersion = await MinecraftUtils.getCurrentMinecraftVersion();
const params = {

View File

@ -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() {