diff --git a/src/app/CommandManager.ts b/src/app/CommandManager.ts index 140a9b8..245cd68 100644 --- a/src/app/CommandManager.ts +++ b/src/app/CommandManager.ts @@ -1,15 +1,14 @@ import * as readline from 'readline'; import * as fs from 'fs'; import * as path from 'path'; -import {Server} from "socket.io"; -import * as os from 'os'; +import { Server } from 'socket.io'; class CommandManager { private commands: Map = new Map(); private rl: readline.Interface; - private io: Server|null = null; + private io: Server | null = null; - public constructor() { + constructor() { this.rl = readline.createInterface({ input: process.stdin, output: process.stdout @@ -17,55 +16,64 @@ class CommandManager { } public async boot(io: Server) { - // Start the command manager - this.io = io as Server; + this.io = io; await this.loadCommands(); console.log('[✅] Command manager loaded'); - - // Start the prompt - await this.startPrompt(); + this.startPrompt(); } - private async startPrompt() { + private startPrompt() { this.rl.question('> ', (command: string) => { this.processCommand(command); this.startPrompt(); }); } - private async processCommand(command: string): Promise { + private async processCommand(command: string): Promise { const [cmd, ...args] = command.trim().toLowerCase().split(' '); if (this.commands.has(cmd)) { this.commands.get(cmd)?.(args, this.io as Server); } else { - switch (cmd) { - case 'exit': - console.log('Goodbye!'); - this.rl.close(); - process.exit(0); - break; - default: - console.log(`Unknown command: ${command}`); - break; - } + this.handleUnknownCommand(cmd); + } + } + + private handleUnknownCommand(command: string) { + switch (command) { + case 'exit': + console.log('Goodbye!'); + this.rl.close(); + process.exit(0); + break; + default: + console.error(`Unknown command: ${command}`); + break; } } private async loadCommands() { 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) { - 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: any) { - console.error('[❌] Failed to load file:', file, error); + for (const file of files) { + await this.loadCommand(commandsDir, file); } + } 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); } }