mirror of
https://git.bits.team/Bits/mod-manager.git
synced 2024-11-10 16:38:23 -05:00
Prevent installing mods that are already installed
This commit is contained in:
parent
1241795b7d
commit
b9c2f22de3
@ -5,6 +5,7 @@ import ModrinthSource from "./sources/modrinth_source.js";
|
|||||||
import ModSource from "./sources/mod_source.js";
|
import ModSource from "./sources/mod_source.js";
|
||||||
import ModNotFoundError from "../errors/mod_not_found_error.js";
|
import ModNotFoundError from "../errors/mod_not_found_error.js";
|
||||||
import {readFileSync, writeFileSync} from "fs";
|
import {readFileSync, writeFileSync} from "fs";
|
||||||
|
import Util from "../util/util.js";
|
||||||
|
|
||||||
|
|
||||||
export default class Mods {
|
export default class Mods {
|
||||||
@ -24,7 +25,7 @@ export default class Mods {
|
|||||||
spinner.start();
|
spinner.start();
|
||||||
|
|
||||||
// Search for the mod
|
// Search for the mod
|
||||||
let id;
|
let id: string | undefined;
|
||||||
try {
|
try {
|
||||||
id = await source.search(mod);
|
id = await source.search(mod);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -39,16 +40,22 @@ export default class Mods {
|
|||||||
|
|
||||||
// If a mod is found, install it
|
// If a mod is found, install it
|
||||||
if (id != undefined) {
|
if (id != undefined) {
|
||||||
spinner.updateText(`Installing ${mod}...`)
|
|
||||||
try {
|
|
||||||
const modObj: Mod = await source.install(id);
|
|
||||||
this.trackMod(modObj);
|
|
||||||
|
|
||||||
spinner.succeed(`Successfully installed ${mod}`);
|
// If mod is not already installed
|
||||||
success = true;
|
if (!this.isModInstalled(id)) {
|
||||||
} catch (e) {
|
spinner.updateText(`Installing ${mod}...`)
|
||||||
// Log the error, and continue to next source
|
try {
|
||||||
spinner.error(e);
|
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 {
|
private static trackMod(mod: Mod): void {
|
||||||
// Parse current file
|
// Read current file
|
||||||
const file = readFileSync(this.getModFilePath(), "utf-8");
|
const mods = this.getTrackedMods();
|
||||||
const json: Array<Mod> = JSON.parse(file);
|
|
||||||
|
|
||||||
// Add mod
|
// Add mod
|
||||||
json.push(mod);
|
mods.push(mod);
|
||||||
|
|
||||||
// Write list back to file
|
// Write list back to file
|
||||||
writeFileSync(this.getModFilePath(), JSON.stringify(json, null, 4));
|
this.writeFile(mods);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getTrackedMods(): Array<Mod> {
|
||||||
|
const file = readFileSync(this.getModFilePath(), "utf-8");
|
||||||
|
return JSON.parse(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static writeFile(mods: Array<Mod>): void {
|
||||||
|
writeFileSync(this.getModFilePath(), JSON.stringify(mods, null, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static isModInstalled(id: string): boolean {
|
||||||
|
const modsFromSource: Array<Mod> = this.getTrackedMods().filter(mod => mod.id == id);
|
||||||
|
return !Util.isArrayEmpty(modsFromSource)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -176,5 +176,4 @@ export default class ModrinthSource implements ModSource {
|
|||||||
return await response.data.title;
|
return await response.data.title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user