#169 : Re-enabled command manager, created extrudeTiles command for testing

This commit is contained in:
2024-09-30 19:02:55 +02:00
parent ddeee356b4
commit 3a83f2c1ff
5 changed files with 169 additions and 32 deletions

View File

@ -2,6 +2,7 @@ import * as readline from 'readline'
import * as fs from 'fs'
import * as path from 'path'
import { Server } from 'socket.io'
import { commandLogger } from '../utilities/logger'
class CommandManager {
private commands: Map<string, Function> = new Map()
@ -23,7 +24,7 @@ class CommandManager {
public async boot(io: Server) {
this.io = io
await this.loadCommands()
console.log('[✅] Command manager loaded')
commandLogger.info('Command manager loaded')
this.startPrompt()
}
@ -48,7 +49,6 @@ class CommandManager {
private handleUnknownCommand(command: string) {
switch (command) {
case 'exit':
console.log('Goodbye!')
this.rl.close()
break
default:
@ -58,7 +58,7 @@ class CommandManager {
}
private async loadCommands() {
const commandsDir = path.resolve(__dirname, 'commands')
const commandsDir = path.resolve(process.cwd(), 'src', 'commands')
try {
const files: string[] = await fs.promises.readdir(commandsDir)
@ -66,7 +66,7 @@ class CommandManager {
await this.loadCommand(commandsDir, file)
}
} catch (error) {
console.error('[❌] Failed to read commands directory:', error)
commandLogger.error(`Failed to read commands directory: ${error}`)
}
}
@ -79,16 +79,16 @@ class CommandManager {
this.registerCommand(commandName, module.default)
} catch (error) {
console.error('[❌] Failed to load command:', file, error)
commandLogger.error(`Failed to load command: ${file}: ${error}`)
}
}
private registerCommand(name: string, command: (args: string[], io: Server) => void) {
if (this.commands.has(name)) {
console.warn(`Command '${name}' is already registered. Overwriting...`)
commandLogger.warn(`Command '${name}' is already registered. Overwriting...`)
}
this.commands.set(name, command)
console.log(`Registered command: ${name}`)
commandLogger.info(`Registered command: ${name}`)
}
}