forked from noxious/server
Renamed command manager to console manager, improved log reading
This commit is contained in:
56
src/managers/consoleManager.ts
Normal file
56
src/managers/consoleManager.ts
Normal 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()
|
Reference in New Issue
Block a user