Added downloading mods from Modrinth

This commit is contained in:
Kallum Jones
2022-08-03 15:00:36 +01:00
parent 7774386385
commit 765920f80c
14 changed files with 681 additions and 11 deletions

View File

@ -0,0 +1,44 @@
import {readdirSync} from "fs";
import path from "path";
import axios from "axios";
export default class MinecraftUtils {
static async getCurrentMinecraftVersion(): Promise<string> {
// Get installed versions as strings
const installedVersions: Array<string> = readdirSync(this.getVersionsFolderPath(), {withFileTypes: true})
.filter(entry => entry.isDirectory())
.map(entry => entry.name);
// Get a list of Minecraft Versions
const response = await axios.get("https://meta.fabricmc.net/v2/versions/game");
const data = await response.data;
const minecraftVersions: Array<string> = [];
for (const ele of data) {
minecraftVersions.push(ele.version);
}
// Find the latest version that is currently installed
let index = Number.MAX_VALUE;
for (let version of installedVersions) {
let currentIndex = minecraftVersions.indexOf(version);
// If this version, is newer than the previous newest, save it's index
if (currentIndex < index) {
index = currentIndex;
}
}
const latestVersion = minecraftVersions[index];
if (latestVersion == undefined) {
throw new Error("There are no Minecraft versions available in this server. Is this a valid server installation?");
}
return latestVersion;
}
static getVersionsFolderPath(): string {
return path.join("versions")
}
}

5
src/util/util.ts Normal file
View File

@ -0,0 +1,5 @@
export default class Util {
static isArrayEmpty(array: Array<any> | undefined): boolean {
return array === undefined || array.length == 0;
}
}