Moved from CJS to ESM + small improvements like logging

This commit is contained in:
Alan Bridgeman 2026-01-07 15:59:40 -06:00
parent 61df35a271
commit c57160e05d
16 changed files with 512 additions and 488 deletions

View file

@ -2,8 +2,10 @@ import path from 'path';
import { Application } from 'express';
import fse from 'fs-extra';
import { BaseController } from './controllers/BaseController';
import { ErrorController } from './controllers/ErrorController';
import { logMessage, LogLevel } from '@BridgemanAccessible/ba-logging';
import { BaseController } from './controllers/BaseController.js';
import { ErrorController } from './controllers/ErrorController.js';
/**
* The Router sets up the routes/endpoints for the App.
@ -87,7 +89,7 @@ export class Router {
// Load the file as a module
const controllerModule = require(controllerPath);
console.log(`${controllerPath} loaded successfully: ${JSON.stringify(Object.keys(controllerModule))} exports found.`)
logMessage(`${controllerPath} loaded successfully: ${JSON.stringify(Object.keys(controllerModule))} exports found.`, LogLevel.DEBUG);
// Get all the classes in the module
const classes = Object.keys(controllerModule)
@ -152,7 +154,7 @@ export class Router {
*/
private getRoutesInController(controller: any) {
if (typeof controller === 'undefined') {
console.error(`Something went wrong while processing ${JSON.stringify(controller)}`);
logMessage(`Something went wrong while processing ${JSON.stringify(controller)}`, LogLevel.ERROR);
throw new Error('The controller must be a class');
}
@ -239,7 +241,7 @@ export class Router {
routes = this.getRoutesInController(controller);
}
catch (e) {
console.error(`Something went wrong while processing ${JSON.stringify(controller)} (${JSON.stringify(decoratedController)})`);
logMessage(`Something went wrong while processing ${JSON.stringify(controller)} (${JSON.stringify(decoratedController)})`, LogLevel.ERROR);
}
routes.GET.forEach((path) => {
@ -315,8 +317,8 @@ export class Router {
// Add the routes for the top level controllers to the list of added routes and call the setup method
addedRoutes = addedRoutes.concat((await Promise.all(topLevelControllers.map(async (decoratedController) => await this.processControllerRoutes(app, decoratedController)))).flat());
console.log('Routes added:');
addedRoutes.forEach(route => console.log('\t' + route));
logMessage('Routes added:', LogLevel.DEBUG);
addedRoutes.forEach(route => logMessage('\t' + route, LogLevel.DEBUG));
// Adding a GET handler/middleware to handle 404 errors
// This is done by adding this as the last path (after all other routes)
@ -340,7 +342,7 @@ export class Router {
let handledErrors: string[] = [];
loadedErrorControllers.forEach(errorController => {
if (typeof errorController.handle === 'undefined') {
console.error(`Something went wrong while processing ${JSON.stringify(Object.entries(errorController))}`);
logMessage(`Something went wrong while processing ${JSON.stringify(Object.entries(errorController))}`, LogLevel.ERROR);
return;
}
@ -348,8 +350,8 @@ export class Router {
handledErrors.push(errorController.handlesError ?? '');
});
console.log('Error controllers added:');
handledErrors.forEach(error => console.log('\t' + error));
logMessage('Error controllers added:', LogLevel.DEBUG);
handledErrors.forEach(errorControllers => logMessage('\t' + errorControllers, LogLevel.DEBUG));
}
/**
@ -358,7 +360,7 @@ export class Router {
* @param app The Express app to add the routes to
*/
async setup(app: Application) {
console.log('Setting up routes...')
logMessage('Setting up routes...', LogLevel.DEBUG);
await this.setupControllers(app);
}