Uhm excuse me, but what the fuck

This commit is contained in:
2024-09-30 22:56:17 +02:00
parent 4f9a1bc879
commit da8ef9fa65
3 changed files with 67 additions and 66 deletions

View File

@ -95,27 +95,37 @@ export class Server {
}
private async loadEventHandlers(dir: string, socket: TSocket) {
const files: Dirent[] = await fs.promises.readdir(dir, { withFileTypes: true })
try {
const files = await fs.promises.readdir(dir, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(dir, file.name)
for (const file of files) {
const fullPath = path.join(dir, file.name);
if (file.isDirectory()) {
await this.loadEventHandlers(fullPath, socket);
continue;
}
if (!file.isFile() || (!file.name.endsWith('.ts') && !file.name.endsWith('.js'))) {
continue;
}
if (file.isDirectory()) {
await this.loadEventHandlers(fullPath, socket)
} else if (file.isFile() && (file.name.endsWith('.ts') || file.name.endsWith('.js'))) {
try {
const module = await import(fullPath)
if (typeof module.default === 'function') {
const EventClass = module.default
const eventInstance = new EventClass(this.io, socket)
eventInstance.listen()
} else {
appLogger.warn(`Unrecognized export in ${file.name}`)
const module = await import(fullPath);
if (typeof module.default !== 'function') {
appLogger.warn(`Unrecognized export in ${file.name}`);
continue;
}
} catch (error: any) {
appLogger.error(`Error loading event handler ${file.name}: ${error.message}`)
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)}`);
}
}
} catch (error) {
appLogger.error(`Error reading directory ${dir}: ${error instanceof Error ? error.message : String(error)}`);
}
}
}