import path from "path"; import Initialiser from "../util/initialiser.js"; import PrintUtils from "../util/print_utils.js"; import ModrinthSource from "./sources/modrinth_source.js"; import ModSource from "./sources/mod_source.js"; import ModNotFoundError from "../errors/mod_not_found_error.js"; export default class Mods { public static readonly MOD_FILE = "mods.json"; private static readonly MOD_SOURCES: Array = [ new ModrinthSource() ]; public static async install(mod: string): Promise { let success: boolean = false; // Go through each mod source for (const source of this.MOD_SOURCES) { // If we have not yet successfully installed the queried mod if (!success) { PrintUtils.info(`Searching for ${mod}...`); // 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()}`); } else { PrintUtils.error(`An error occurred searching for ${mod} on ${source.getName()}. Skipping ${source.getName()}`, e) // Try the next source continue; } } // If a mod is found, install it if (id != undefined) { PrintUtils.info(`Installing ${mod}...`); try { await source.install(id); PrintUtils.success(`Successfully installed ${mod}`); } catch (e) { // Log the error, and continue to next source PrintUtils.error(e); } } } } } public static getModFilePath(): string { return path.join(Initialiser.getModManagerFolderPath(), this.MOD_FILE); } }