1
0
forked from noxious/server

Renamed command manager to console manager, improved log reading

This commit is contained in:
2024-12-28 21:24:59 +01:00
parent 0b99d4098e
commit 4f1b9cf024
8 changed files with 236 additions and 114 deletions

View File

@ -0,0 +1,56 @@
import { Server } from 'socket.io'
import { CommandRegistry } from '#application/console/commandRegistry'
import { ConsolePrompt } from '#application/console/consolePrompt'
import { LogReader } from '#application/console/logReader'
import Logger, { LoggerType } from '#application/logger'
export class ConsoleManager {
private readonly logger = Logger.type(LoggerType.COMMAND)
private readonly registry: CommandRegistry
private readonly prompt: ConsolePrompt
private readonly logReader: LogReader
private io: Server | null = null
constructor() {
this.registry = new CommandRegistry()
this.prompt = new ConsolePrompt(
(command: string) => this.processCommand(command)
)
this.logReader = new LogReader(process.cwd())
}
public async boot(io: Server): Promise<void> {
this.io = io
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(this.io as Server)
await commandInstance.execute(args)
} catch (error) {
this.logger.error(`Error executing command ${cmd}: ${error instanceof Error ? error.message : String(error)}`)
}
}
}
export default new ConsoleManager()