2022-08-03 11:52:24 -04:00
|
|
|
import {existsSync, mkdirSync, writeFileSync} from "fs";
|
2022-08-02 10:41:41 -04:00
|
|
|
import path from "path";
|
2022-08-01 09:36:20 -04:00
|
|
|
import PrintUtils from "./print_utils.js";
|
2022-08-03 11:52:24 -04:00
|
|
|
import ModManager from "../mod-manager.js";
|
2022-08-05 11:56:04 -04:00
|
|
|
import inquirer from "inquirer";
|
|
|
|
import MinecraftUtils from "./minecraft_utils.js";
|
|
|
|
import MinecraftVersionError from "../errors/minecraft_version_error.js";
|
2022-08-01 09:36:20 -04:00
|
|
|
|
|
|
|
export default class Initialiser {
|
2022-08-05 11:56:04 -04:00
|
|
|
public static async initialise(): Promise<void> {
|
2022-08-01 09:36:20 -04:00
|
|
|
if (!this.isInitialised()) {
|
|
|
|
if (this.isDirFabricServer()) {
|
|
|
|
const success: boolean = this.setupFolderStructure();
|
|
|
|
|
|
|
|
if (success) {
|
2022-08-05 11:56:04 -04:00
|
|
|
const version = await this.getMinecraftVersionFromInput();
|
|
|
|
await MinecraftUtils.updateCurrentMinecraftVersion(version)
|
|
|
|
|
2022-08-01 09:36:20 -04:00
|
|
|
PrintUtils.success("Sucessfully initialised Mod Manager!");
|
2022-08-03 11:52:24 -04:00
|
|
|
|
|
|
|
// Initialise a logger when Mod Manager is initialised
|
|
|
|
if (ModManager.logger == null) {
|
|
|
|
ModManager.logger = ModManager.createLogger();
|
|
|
|
}
|
|
|
|
|
2022-08-01 09:36:20 -04:00
|
|
|
} else {
|
|
|
|
PrintUtils.error("Unable to set up the Mod Manager folder structure");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PrintUtils.error("Unable to initialise Mod Manager as this is not a Fabric Minecraft Server");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PrintUtils.warn("Mod Manager is already initialised!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static isInitialised(): boolean {
|
2022-08-05 11:56:04 -04:00
|
|
|
return existsSync(ModManager.FilePaths.MOD_MANAGER_FOLDER_PATH);
|
2022-08-03 11:55:12 -04:00
|
|
|
}
|
|
|
|
|
2022-08-01 09:36:20 -04:00
|
|
|
private static isDirFabricServer(): boolean {
|
2022-08-02 10:41:41 -04:00
|
|
|
const serverProperties = path.join("server.properties");
|
|
|
|
const fabric = path.join(".fabric");
|
2022-08-01 09:36:20 -04:00
|
|
|
|
|
|
|
return existsSync(serverProperties) && existsSync(fabric);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static setupFolderStructure(): boolean {
|
2022-08-05 11:56:04 -04:00
|
|
|
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, "")
|
2022-08-01 09:36:20 -04:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 11:56:04 -04:00
|
|
|
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;
|
|
|
|
}
|
2022-08-01 09:36:20 -04:00
|
|
|
}
|