mod-manager/src/mod-manager.ts

116 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2022-08-01 13:36:20 +00:00
#!/usr/bin/env node
2022-08-03 15:52:24 +00:00
import {Command} from "commander";
2022-08-01 13:36:20 +00:00
import InitCommand from "./commands/init_command.js";
2022-08-03 14:00:36 +00:00
import InstallCommand from "./commands/install_command.js";
2022-08-01 13:36:20 +00:00
import Subcommand from "./commands/subcommand.js";
import Initialiser from "./util/initialiser.js";
import PrintUtils from "./util/print_utils.js";
2022-08-03 15:52:24 +00:00
import path from "path";
import {Logger, pino} from "pino"
2022-08-03 19:42:51 +00:00
import {ListCommand} from "./commands/list_command.js";
2022-08-04 13:24:39 +00:00
import UninstallCommand from "./commands/uninstall_command.js";
import EssentialCommand from "./commands/essential_command.js";
2022-08-04 15:15:19 +00:00
import {readFileSync, unlinkSync} from "fs";
2022-08-09 12:50:03 +00:00
import UpdateCommand from "./commands/update_command.js";
2022-08-07 19:09:42 +00:00
import MigratePossibleCommand from "./commands/migrate_possible.js";
2022-08-08 13:14:30 +00:00
import MigrateCommand from "./commands/migrate_command.js";
import ModrinthSource from "./mods/sources/modrinth_source.js";
import Mods from "./mods/mods.js";
2022-08-09 13:55:54 +00:00
import {CurseforgeSource} from "./mods/sources/curseforge_source.js";
2022-08-09 22:17:08 +00:00
import MinecraftUtils from "./util/minecraft_utils.js";
import chalk from "chalk";
2024-04-06 18:50:52 +00:00
import ForgejoSource from "./mods/sources/forgejo_source.js";
2022-08-01 13:36:20 +00:00
export default class ModManager {
2022-08-03 15:55:12 +00:00
public static logger: Logger | null = null;
2022-08-09 13:56:46 +00:00
static FilePaths = class {
public static readonly MOD_MANAGER_FOLDER_PATH = path.join(".mod-manager");
public static readonly LOGS_FOLDER = path.join(this.MOD_MANAGER_FOLDER_PATH, "logs");
public static readonly LOG_FILE: string = path.join(this.LOGS_FOLDER, `${new Date().valueOf()}.log.json`);
public static readonly MOD_FILE_PATH = path.join(this.MOD_MANAGER_FOLDER_PATH, "mods.json");
public static readonly VERSION_FILE_PATH = path.join(this.MOD_MANAGER_FOLDER_PATH, "version")
public static readonly MODS_FOLDER_PATH = path.join("mods")
}
2022-08-03 15:55:12 +00:00
private static program: Command = new Command();
private static subcommands: Array<Subcommand> = [
new InitCommand(),
2022-08-03 19:42:51 +00:00
new InstallCommand(),
2022-08-04 13:24:39 +00:00
new ListCommand(),
new UninstallCommand(),
2022-08-07 13:03:34 +00:00
new EssentialCommand(),
new UpdateCommand(),
2022-08-08 13:14:30 +00:00
new MigratePossibleCommand(),
new MigrateCommand()
2022-08-03 15:55:12 +00:00
];
2022-08-01 13:36:20 +00:00
2022-08-09 22:17:08 +00:00
static async init() {
2022-08-03 15:55:12 +00:00
if (Initialiser.isInitialised()) {
this.logger = ModManager.createLogger();
}
2022-08-03 15:52:24 +00:00
const version = Initialiser.isInitialised() ?
await MinecraftUtils.getCurrentMinecraftVersion() :
chalk.redBright("Not Initialised. See `mod-manager init -h` for more details!")
2022-08-03 15:55:12 +00:00
this.program
.name('mod-manager')
2022-08-09 22:17:08 +00:00
.description('A package (mod) manager for Fabric Minecraft Servers')
.version(`Minecraft server version: ${version}`, "-v, --version", "Reports the version of the Minecraft server");
2022-08-09 22:17:08 +00:00
2022-08-03 14:00:36 +00:00
2022-08-03 15:55:12 +00:00
for (const command of this.subcommands) {
command.registerCommand(this.program);
}
2022-08-01 13:36:20 +00:00
Mods.registerSource(new ModrinthSource())
2022-08-09 13:55:54 +00:00
Mods.registerSource(new CurseforgeSource(), "CURSEFORGE_API_KEY")
2024-04-06 18:50:52 +00:00
Mods.registerSource(new ForgejoSource(), "FORGEJO_API_KEY")
2022-08-07 13:03:34 +00:00
this.program.showSuggestionAfterError();
this.program.showHelpAfterError();
2022-08-09 22:17:08 +00:00
2022-08-03 15:55:12 +00:00
this.program.parse();
2022-08-03 14:00:36 +00:00
}
2022-08-01 13:36:20 +00:00
2022-08-03 15:55:12 +00:00
static execute(callback: () => any): void {
if (Initialiser.isInitialised()) {
callback();
} else {
PrintUtils.error("Mod Manager is not initialised");
}
2022-08-01 13:36:20 +00:00
}
2022-08-03 15:52:24 +00:00
2022-08-03 15:55:12 +00:00
static createLogger(): Logger {
2022-08-04 15:38:25 +00:00
let logger = pino({
2022-08-09 13:56:46 +00:00
base: {
pid: undefined,
hostname: undefined
}
},
2022-08-04 15:38:25 +00:00
pino.destination({
dest: ModManager.FilePaths.LOG_FILE,
2022-08-04 15:38:25 +00:00
sync: true
})
);
2022-08-03 15:55:12 +00:00
process.on("uncaughtException", error => {
2022-08-05 15:17:35 +00:00
PrintUtils.error(`[FATAL]: ${error.message}`, error);
2022-08-03 15:55:12 +00:00
setTimeout(() => process.exit(1), 1)
})
2022-08-03 15:52:24 +00:00
2022-08-04 15:15:19 +00:00
// If no errors are logged, cleanup the log file when the process exits
process.on("exit", () => {
// If file is only whitespace, i.e. blank
if (!readFileSync(ModManager.FilePaths.LOG_FILE, "utf-8")?.trim().length) {
unlinkSync(ModManager.FilePaths.LOG_FILE)
2022-08-04 15:15:19 +00:00
}
})
2022-08-03 15:55:12 +00:00
return logger;
}
2022-08-01 13:36:20 +00:00
}
ModManager.init();