1
0
forked from noxious/server
noxious_server/src/managers/consoleManager.ts

52 lines
1.5 KiB
TypeScript

import { CommandRegistry } from '#application/console/commandRegistry'
import { ConsolePrompt } from '#application/console/consolePrompt'
import { LogReader } from '#application/console/logReader'
import Logger, { LoggerType } from '#application/logger'
import SocketManager from '#managers/socketManager'
export class ConsoleManager {
private readonly logger = Logger.type(LoggerType.COMMAND)
private readonly registry: CommandRegistry
private readonly prompt: ConsolePrompt
private readonly logReader: LogReader
constructor() {
this.registry = new CommandRegistry()
this.prompt = new ConsolePrompt((command: string) => this.processCommand(command))
this.logReader = new LogReader(process.cwd())
}
public async boot(): Promise<void> {
await this.registry.loadCommands()
this.logReader.start()
this.prompt.start()
this.logger.info('Console manager loaded')
}
private async processCommand(commandLine: string): Promise<void> {
const [cmd, ...args] = commandLine.trim().split(' ')
if (cmd === 'exit') {
this.prompt.close()
return
}
const CommandClass = this.registry.getCommand(cmd)
if (!CommandClass) {
console.error(`Unknown command: ${cmd}`)
return
}
try {
const commandInstance = new CommandClass(SocketManager.getIO())
await commandInstance.execute(args)
} catch (error) {
this.logger.error(`Error executing command ${cmd}: ${error instanceof Error ? error.message : String(error)}`)
}
}
}
export default new ConsoleManager()