From b9c2f22de38aff13dffa63edce6f641db80269f8 Mon Sep 17 00:00:00 2001 From: Kallum Jones Date: Wed, 3 Aug 2022 20:16:19 +0100 Subject: [PATCH] Prevent installing mods that are already installed --- src/mods/mods.ts | 50 ++++++++++++++++++++--------- src/mods/sources/modrinth_source.ts | 1 - 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/mods/mods.ts b/src/mods/mods.ts index 4122635..4653922 100644 --- a/src/mods/mods.ts +++ b/src/mods/mods.ts @@ -5,6 +5,7 @@ import ModrinthSource from "./sources/modrinth_source.js"; import ModSource from "./sources/mod_source.js"; import ModNotFoundError from "../errors/mod_not_found_error.js"; import {readFileSync, writeFileSync} from "fs"; +import Util from "../util/util.js"; export default class Mods { @@ -24,7 +25,7 @@ export default class Mods { spinner.start(); // Search for the mod - let id; + let id: string | undefined; try { id = await source.search(mod); } catch (e) { @@ -39,16 +40,22 @@ export default class Mods { // If a mod is found, install it if (id != undefined) { - spinner.updateText(`Installing ${mod}...`) - try { - const modObj: Mod = await source.install(id); - this.trackMod(modObj); - spinner.succeed(`Successfully installed ${mod}`); - success = true; - } catch (e) { - // Log the error, and continue to next source - spinner.error(e); + // If mod is not already installed + if (!this.isModInstalled(id)) { + spinner.updateText(`Installing ${mod}...`) + try { + const modObj: Mod = await source.install(id); + this.trackMod(modObj); + + spinner.succeed(`Successfully installed ${mod}`); + success = true; + } catch (e) { + // Log the error, and continue to next source + spinner.error(e); + } + } else { + spinner.error(`Mod ${mod} is already installed.`) } } } @@ -60,14 +67,27 @@ export default class Mods { } private static trackMod(mod: Mod): void { - // Parse current file - const file = readFileSync(this.getModFilePath(), "utf-8"); - const json: Array = JSON.parse(file); + // Read current file + const mods = this.getTrackedMods(); // Add mod - json.push(mod); + mods.push(mod); // Write list back to file - writeFileSync(this.getModFilePath(), JSON.stringify(json, null, 4)); + this.writeFile(mods); + } + + public static getTrackedMods(): Array { + const file = readFileSync(this.getModFilePath(), "utf-8"); + return JSON.parse(file); + } + + public static writeFile(mods: Array): void { + writeFileSync(this.getModFilePath(), JSON.stringify(mods, null, 4)); + } + + private static isModInstalled(id: string): boolean { + const modsFromSource: Array = this.getTrackedMods().filter(mod => mod.id == id); + return !Util.isArrayEmpty(modsFromSource) } } \ No newline at end of file diff --git a/src/mods/sources/modrinth_source.ts b/src/mods/sources/modrinth_source.ts index 1d30b95..434a549 100644 --- a/src/mods/sources/modrinth_source.ts +++ b/src/mods/sources/modrinth_source.ts @@ -176,5 +176,4 @@ export default class ModrinthSource implements ModSource { return await response.data.title; } - } \ No newline at end of file