mirror of
https://git.bits.team/Bits/mod-manager.git
synced 2025-06-29 16:49:43 -04:00
Initial Commit
This commit is contained in:
16
src/commands/init_command.ts
Normal file
16
src/commands/init_command.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { Command } from "commander";
|
||||
import Initialiser from "../util/initialiser.js";
|
||||
import Subcommand from "./subcommand.js";
|
||||
|
||||
export default class InitCommand extends Subcommand {
|
||||
registerCommand(program: Command) {
|
||||
program.command("init")
|
||||
.description("Initalises mod manager")
|
||||
.action(this.execute);
|
||||
}
|
||||
|
||||
execute() {
|
||||
Initialiser.initialise();
|
||||
}
|
||||
|
||||
}
|
6
src/commands/subcommand.ts
Normal file
6
src/commands/subcommand.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { Command } from "commander";
|
||||
|
||||
export default abstract class Subcommand {
|
||||
abstract registerCommand(program: Command): void;
|
||||
abstract execute(): void;
|
||||
}
|
39
src/mod-manager.ts
Normal file
39
src/mod-manager.ts
Normal file
@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env node
|
||||
import { Command } from "commander";
|
||||
import InitCommand from "./commands/init_command.js";
|
||||
import Subcommand from "./commands/subcommand.js";
|
||||
import Initialiser from "./util/initialiser.js";
|
||||
import PrintUtils from "./util/print_utils.js";
|
||||
|
||||
export default class ModManager {
|
||||
private static program: Command = new Command();
|
||||
|
||||
private static subcommands: Array<Subcommand> = [
|
||||
new InitCommand()
|
||||
];
|
||||
|
||||
static init() {
|
||||
this.program
|
||||
.name('mod-manager')
|
||||
.description('A package (mod) manager for Fabric Minecraft Servers');
|
||||
|
||||
this.subcommands.forEach(command => {
|
||||
command.registerCommand(ModManager.program);
|
||||
});
|
||||
|
||||
this.program.parse();
|
||||
}
|
||||
|
||||
static execute(callback: () => any): void {
|
||||
if (Initialiser.isInitialised()) {
|
||||
callback();
|
||||
} else {
|
||||
PrintUtils.error("Mod Manager is not initialised");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ModManager.init();
|
||||
|
||||
|
53
src/util/initialiser.ts
Normal file
53
src/util/initialiser.ts
Normal file
@ -0,0 +1,53 @@
|
||||
import { existsSync, mkdirSync } from "fs";
|
||||
import PrintUtils from "./print_utils.js";
|
||||
|
||||
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!");
|
||||
} 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 {
|
||||
const workingDirectory = process.cwd();
|
||||
|
||||
const serverProperties = `${workingDirectory}/server.properties`;
|
||||
const fabric = `${workingDirectory}/.fabric`;
|
||||
|
||||
return existsSync(serverProperties) && existsSync(fabric);
|
||||
}
|
||||
|
||||
private static setupFolderStructure(): boolean {
|
||||
if (!existsSync(this.getModManagerFolderPath())) {
|
||||
mkdirSync(this.getModManagerFolderPath());
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static getModManagerFolderPath(): string {
|
||||
const workingDirectory = process.cwd();
|
||||
|
||||
return `${workingDirectory}/${this.MOD_MANAGER_FOLDER}`;
|
||||
}
|
||||
|
||||
}
|
19
src/util/print_utils.ts
Normal file
19
src/util/print_utils.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import chalk from "chalk";
|
||||
|
||||
export default class PrintUtils {
|
||||
static info(print: string) {
|
||||
console.log(chalk.white(print));
|
||||
}
|
||||
|
||||
static warn(print: string) {
|
||||
console.log(chalk.yellowBright(print));
|
||||
}
|
||||
|
||||
static success(print: string) {
|
||||
console.log(chalk.greenBright(print));
|
||||
}
|
||||
|
||||
static error(print: string) {
|
||||
console.log(chalk.redBright(print));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user