mirror of
https://git.bits.team/Bits/mod-manager.git
synced 2025-06-30 17:19:43 -04:00
Changed Minecraft Version tracking to manual input rather than auto-detecting
This commit is contained in:
@ -1,18 +1,21 @@
|
||||
import {existsSync, mkdirSync, writeFileSync} from "fs";
|
||||
import path from "path";
|
||||
import Mods from "../mods/mods.js";
|
||||
import PrintUtils from "./print_utils.js";
|
||||
import ModManager from "../mod-manager.js";
|
||||
import inquirer from "inquirer";
|
||||
import MinecraftUtils from "./minecraft_utils.js";
|
||||
import MinecraftVersionError from "../errors/minecraft_version_error.js";
|
||||
|
||||
export default class Initialiser {
|
||||
private static readonly MOD_MANAGER_FOLDER = ".mod-manager"
|
||||
|
||||
public static initialise(): void {
|
||||
public static async initialise(): Promise<void> {
|
||||
if (!this.isInitialised()) {
|
||||
if (this.isDirFabricServer()) {
|
||||
const success: boolean = this.setupFolderStructure();
|
||||
|
||||
if (success) {
|
||||
const version = await this.getMinecraftVersionFromInput();
|
||||
await MinecraftUtils.updateCurrentMinecraftVersion(version)
|
||||
|
||||
PrintUtils.success("Sucessfully initialised Mod Manager!");
|
||||
|
||||
// Initialise a logger when Mod Manager is initialised
|
||||
@ -32,11 +35,7 @@ export default class Initialiser {
|
||||
}
|
||||
|
||||
public static isInitialised(): boolean {
|
||||
return existsSync(this.getModManagerFolderPath());
|
||||
}
|
||||
|
||||
public static getModManagerFolderPath(): string {
|
||||
return path.join(this.MOD_MANAGER_FOLDER);
|
||||
return existsSync(ModManager.FilePaths.MOD_MANAGER_FOLDER_PATH);
|
||||
}
|
||||
|
||||
private static isDirFabricServer(): boolean {
|
||||
@ -47,14 +46,40 @@ export default class Initialiser {
|
||||
}
|
||||
|
||||
private static setupFolderStructure(): boolean {
|
||||
if (!existsSync(this.getModManagerFolderPath())) {
|
||||
mkdirSync(this.getModManagerFolderPath());
|
||||
writeFileSync(Mods.getModFilePath(), "[]");
|
||||
mkdirSync(path.join(this.getModManagerFolderPath(), "logs"))
|
||||
if (!existsSync(ModManager.FilePaths.MOD_MANAGER_FOLDER_PATH)) {
|
||||
mkdirSync(ModManager.FilePaths.MOD_MANAGER_FOLDER_PATH);
|
||||
writeFileSync(ModManager.FilePaths.MOD_FILE_PATH, "[]");
|
||||
mkdirSync(ModManager.FilePaths.LOGS_FOLDER)
|
||||
writeFileSync(ModManager.FilePaths.VERSION_FILE_PATH, "")
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static async getMinecraftVersionFromInput() {
|
||||
let isVersionValid = false;
|
||||
|
||||
let version: string | undefined = undefined;
|
||||
while (!isVersionValid) {
|
||||
const answer = await inquirer.prompt([{
|
||||
type: "input",
|
||||
name: "minecraft_version",
|
||||
message: "What version of Minecraft is the server running?"
|
||||
}])
|
||||
version = answer.minecraft_version;
|
||||
|
||||
if (await MinecraftUtils.isValidVersion(version)) {
|
||||
isVersionValid = true;
|
||||
} else {
|
||||
PrintUtils.error(`${version} is not a valid Minecraft version for a Fabric server. Please try again`);
|
||||
}
|
||||
}
|
||||
|
||||
if (version == undefined) {
|
||||
throw new MinecraftVersionError("Escaped version input without a valid version")
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
}
|
@ -1,14 +1,19 @@
|
||||
import {readdirSync} from "fs";
|
||||
import path from "path";
|
||||
import {readFileSync, writeFileSync} from "fs";
|
||||
import axios from "axios";
|
||||
import ModManager from "../mod-manager.js";
|
||||
import MinecraftVersionError from "../errors/minecraft_version_error.js";
|
||||
|
||||
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);
|
||||
return readFileSync(ModManager.FilePaths.VERSION_FILE_PATH, "utf-8");
|
||||
}
|
||||
|
||||
static async isValidVersion(version: any) {
|
||||
const allMinecraftVersions = await this.getAllMinecraftVersions();
|
||||
return allMinecraftVersions.includes(version);
|
||||
}
|
||||
|
||||
static async getAllMinecraftVersions(): Promise<Array<string>> {
|
||||
// Get a list of Minecraft Versions
|
||||
const response = await axios.get("https://meta.fabricmc.net/v2/versions/game");
|
||||
const data = await response.data;
|
||||
@ -18,27 +23,16 @@ export default class MinecraftUtils {
|
||||
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;
|
||||
return minecraftVersions;
|
||||
}
|
||||
|
||||
static getVersionsFolderPath(): string {
|
||||
return path.join("versions")
|
||||
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}`)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user