1
0
forked from noxious/server

More command manager improvements

This commit is contained in:
Dennis Postma 2024-06-02 20:28:04 +02:00
parent 25251d5012
commit 4216dc567a

View File

@ -1,15 +1,14 @@
import * as readline from 'readline'; import * as readline from 'readline';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import {Server} from "socket.io"; import { Server } from 'socket.io';
import * as os from 'os';
class CommandManager { class CommandManager {
private commands: Map<string, Function> = new Map(); private commands: Map<string, Function> = new Map();
private rl: readline.Interface; private rl: readline.Interface;
private io: Server|null = null; private io: Server | null = null;
public constructor() { constructor() {
this.rl = readline.createInterface({ this.rl = readline.createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout output: process.stdout
@ -17,55 +16,64 @@ class CommandManager {
} }
public async boot(io: Server) { public async boot(io: Server) {
// Start the command manager this.io = io;
this.io = io as Server;
await this.loadCommands(); await this.loadCommands();
console.log('[✅] Command manager loaded'); console.log('[✅] Command manager loaded');
this.startPrompt();
// Start the prompt
await this.startPrompt();
} }
private async startPrompt() { private startPrompt() {
this.rl.question('> ', (command: string) => { this.rl.question('> ', (command: string) => {
this.processCommand(command); this.processCommand(command);
this.startPrompt(); this.startPrompt();
}); });
} }
private async processCommand(command: string): Promise<any> { private async processCommand(command: string): Promise<void> {
const [cmd, ...args] = command.trim().toLowerCase().split(' '); const [cmd, ...args] = command.trim().toLowerCase().split(' ');
if (this.commands.has(cmd)) { if (this.commands.has(cmd)) {
this.commands.get(cmd)?.(args, this.io as Server); this.commands.get(cmd)?.(args, this.io as Server);
} else { } else {
switch (cmd) { this.handleUnknownCommand(cmd);
case 'exit': }
console.log('Goodbye!'); }
this.rl.close();
process.exit(0); private handleUnknownCommand(command: string) {
break; switch (command) {
default: case 'exit':
console.log(`Unknown command: ${command}`); console.log('Goodbye!');
break; this.rl.close();
} process.exit(0);
break;
default:
console.error(`Unknown command: ${command}`);
break;
} }
} }
private async loadCommands() { private async loadCommands() {
const commandsDir = path.resolve(__dirname, 'commands'); const commandsDir = path.resolve(__dirname, 'commands');
const files: string[] = await fs.promises.readdir(commandsDir); try {
const files: string[] = await fs.promises.readdir(commandsDir);
for (const file of files) { for (const file of files) {
try { await this.loadCommand(commandsDir, file);
const ext = path.extname(file);
const commandName = path.basename(file, ext);
const commandPath = path.join(commandsDir, file);
const module = await import(commandPath);
this.registerCommand(commandName, module.default);
} catch (error: any) {
console.error('[❌] Failed to load file:', file, error);
} }
} catch (error) {
console.error('[❌] Failed to read commands directory:', error);
}
}
private async loadCommand(commandsDir: string, file: string) {
try {
const ext = path.extname(file);
const commandName = path.basename(file, ext);
const commandPath = path.join(commandsDir, file);
const module = await import(commandPath);
this.registerCommand(commandName, module.default);
} catch (error) {
console.error('[❌] Failed to load command:', file, error);
} }
} }