Add install by id functionality

This commit is contained in:
Kallum Jones 2022-08-14 14:59:41 +01:00
parent 8398afcfb4
commit 4e744d11fd
No known key found for this signature in database
GPG Key ID: D7F4589C4D7F81A9

View File

@ -23,16 +23,30 @@ export default class Mods {
this.MOD_SOURCES.push(source); this.MOD_SOURCES.push(source);
} }
private static async isValidModId(id: string, source: ModSource) {
const mcVersion = await MinecraftUtils.getCurrentMinecraftVersion();
try {
await source.getLatestVersion(id, mcVersion);
return true;
} catch (e) {
return false;
}
}
public static async install(mod: string, essential: boolean, confirm: boolean): Promise<void> { public static async install(mod: string, essential: boolean, confirm: boolean): Promise<void> {
// Go through each mod source // Go through each mod source
for (const source of this.MOD_SOURCES) { for (const source of this.MOD_SOURCES) {
const spinner = new PrintUtils.Spinner(`Searching for ${mod}...`); const spinner = new PrintUtils.Spinner(`Searching for ${mod}...`);
spinner.start(); spinner.start();
// Search for the mod // Determine queried mod id
let id: string | undefined; let id: string | undefined;
try { try {
id = await source.search(mod); if (await this.isValidModId(mod, source)) {
id = mod;
} else {
id = await source.search(mod);
}
} catch (e) { } catch (e) {
if (e instanceof ModNotFoundError) { if (e instanceof ModNotFoundError) {
spinner.stop(`Mod ${mod} not found on ${source.getSourceName()}`) spinner.stop(`Mod ${mod} not found on ${source.getSourceName()}`)