mod-manager/src/util/initialiser.ts

60 lines
2.0 KiB
TypeScript
Raw Normal View History

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";
import Mods from "../mods/mods.js";
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-01 09:36:20 -04:00
export default class Initialiser {
private static readonly MOD_MANAGER_FOLDER = ".mod-manager"
public static initialise(): void {
if (!this.isInitialised()) {
if (this.isDirFabricServer()) {
const success: boolean = this.setupFolderStructure();
if (success) {
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 {
return existsSync(this.getModManagerFolderPath());
}
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 {
if (!existsSync(this.getModManagerFolderPath())) {
mkdirSync(this.getModManagerFolderPath());
2022-08-02 10:41:41 -04:00
writeFileSync(Mods.getModFilePath(), "[]");
2022-08-03 11:52:24 -04:00
mkdirSync(path.join(this.getModManagerFolderPath(), "logs"))
2022-08-01 09:36:20 -04:00
return true;
} else {
return false;
}
}
2022-08-02 10:41:41 -04:00
public static getModManagerFolderPath(): string {
return path.join(this.MOD_MANAGER_FOLDER);
2022-08-01 09:36:20 -04:00
}
}