Path fixes for all environments, npm run format, removed redundant imports

This commit is contained in:
2024-10-01 00:10:30 +02:00
parent 4cbd62cbb0
commit ce1708a55e
24 changed files with 82 additions and 87 deletions

View File

@ -1,5 +1,4 @@
import fs from 'fs'
import path from 'path'
import express, { Application } from 'express'
import config from './utilities/config'
import { createServer as httpServer, Server as HTTPServer } from 'http'
@ -9,13 +8,13 @@ import { Server as SocketServer } from 'socket.io'
import { Authentication } from './middleware/authentication'
import { TSocket } from './utilities/types'
import prisma from './utilities/prisma'
import { Dirent } from 'node:fs'
import { appLogger, watchLogs } from './utilities/logger'
import ZoneManager from './managers/zoneManager'
import UserManager from './managers/userManager'
import CommandManager from './managers/commandManager'
import CharacterManager from './managers/characterManager'
import QueueManager from './managers/queueManager'
import { getAppPath } from './utilities/utilities'
export class Server {
private readonly app: Application
@ -86,46 +85,46 @@ export class Server {
* @private
*/
private async handleConnection(socket: TSocket) {
const eventsPath = path.join(__dirname, 'socketEvents')
try {
await this.loadEventHandlers(eventsPath, socket)
await this.loadEventHandlers('socketEvents', '', socket)
} catch (error: any) {
appLogger.error(`Failed to load event handlers: ${error.message}`)
}
}
private async loadEventHandlers(dir: string, socket: TSocket) {
private async loadEventHandlers(baseDir: string, subDir: string, socket: TSocket) {
try {
const files = await fs.promises.readdir(dir, { withFileTypes: true });
const fullDir = getAppPath(baseDir, subDir)
const files = await fs.promises.readdir(fullDir, { withFileTypes: true })
for (const file of files) {
const fullPath = path.join(dir, file.name);
const filePath = getAppPath(baseDir, subDir, file.name)
if (file.isDirectory()) {
await this.loadEventHandlers(fullPath, socket);
continue;
await this.loadEventHandlers(baseDir, `${subDir}/${file.name}`, socket)
continue
}
if (!file.isFile() || (!file.name.endsWith('.ts') && !file.name.endsWith('.js'))) {
continue;
continue
}
try {
const module = await import(fullPath);
const module = await import(filePath)
if (typeof module.default !== 'function') {
appLogger.warn(`Unrecognized export in ${file.name}`);
continue;
appLogger.warn(`Unrecognized export in ${file.name}`)
continue
}
const EventClass = module.default;
const eventInstance = new EventClass(this.io, socket);
eventInstance.listen();
const EventClass = module.default
const eventInstance = new EventClass(this.io, socket)
eventInstance.listen()
} catch (error) {
appLogger.error(`Error loading event handler ${file.name}: ${error instanceof Error ? error.message : String(error)}`);
appLogger.error(`Error loading event handler ${file.name}: ${error instanceof Error ? error.message : String(error)}`)
}
}
} catch (error) {
appLogger.error(`Error reading directory ${dir}: ${error instanceof Error ? error.message : String(error)}`);
appLogger.error(`Error reading directory: ${error instanceof Error ? error.message : String(error)}`)
}
}
}