2022-08-05 11:56:04 -04:00
|
|
|
import {readFileSync, writeFileSync} from "fs";
|
2022-08-03 10:00:36 -04:00
|
|
|
import axios from "axios";
|
2022-08-05 11:56:04 -04:00
|
|
|
import ModManager from "../mod-manager.js";
|
|
|
|
import MinecraftVersionError from "../errors/minecraft_version_error.js";
|
2022-08-03 10:00:36 -04:00
|
|
|
|
|
|
|
export default class MinecraftUtils {
|
|
|
|
static async getCurrentMinecraftVersion(): Promise<string> {
|
2022-08-05 11:56:04 -04:00
|
|
|
return readFileSync(ModManager.FilePaths.VERSION_FILE_PATH, "utf-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
static async isValidVersion(version: any) {
|
|
|
|
const allMinecraftVersions = await this.getAllMinecraftVersions();
|
|
|
|
return allMinecraftVersions.includes(version);
|
|
|
|
}
|
2022-08-03 10:00:36 -04:00
|
|
|
|
2022-08-05 11:56:04 -04:00
|
|
|
static async getAllMinecraftVersions(): Promise<Array<string>> {
|
2022-08-03 10:00:36 -04:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2022-08-05 11:56:04 -04:00
|
|
|
return minecraftVersions;
|
|
|
|
}
|
2022-08-03 10:00:36 -04:00
|
|
|
|
2022-08-05 11:56:04 -04:00
|
|
|
static async updateCurrentMinecraftVersion(version: string) {
|
|
|
|
if (await MinecraftUtils.isValidVersion(version)) {
|
|
|
|
writeFileSync(ModManager.FilePaths.VERSION_FILE_PATH, version);
|
|
|
|
} else {
|
|
|
|
throw new MinecraftVersionError(`Attempted to update version file with invalid version: ${version}`)
|
2022-08-03 10:00:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|