-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setup.js
37 lines (35 loc) · 1.08 KB
/
Setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import fs from "fs";
import BaseController from "./server/utils/BaseController";
export class Paths {
static get Public() {
return __dirname + "/client/";
}
static get Server() {
return __dirname + "/server";
}
static get Controllers() {
return this.Server + "/controllers";
}
}
export function RegisterControllers(router) {
let controllers = fs.readdirSync(Paths.Controllers);
controllers.forEach(loadController);
async function loadController(controllerName) {
try {
if (!controllerName.endsWith(".js")) return;
// @ts-ignore
let fileHandler = await import(Paths.Controllers + "/" + controllerName);
let controllerClass = fileHandler[controllerName.slice(0, -3)];
let controller = new controllerClass();
if (controller instanceof BaseController) {
router.use(controller.mount, controller.router);
}
} catch (e) {
console.error(
"[CONTROLLER ERROR] unable to load controller, potential duplication, review mount path and controller class name",
controllerName,
e
);
}
}
}