1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const fs_1 = __importDefault(require("fs"));
|
7 | const path_1 = __importDefault(require("path"));
|
8 | const ServiceManager_1 = require("../ServiceManager");
|
9 | class ControllerManager extends ServiceManager_1.AbstractPluginManager {
|
10 | constructor(creationContext, config) {
|
11 | super(creationContext, config.controllers);
|
12 | if (config.locations) {
|
13 | this.loadFromLocations(config.locations);
|
14 | }
|
15 | }
|
16 | static getControllerName(controller) {
|
17 | if (typeof controller === 'string') {
|
18 | return controller;
|
19 | }
|
20 | return controller.name;
|
21 | }
|
22 | loadFromLocations(controllerDirectories) {
|
23 | controllerDirectories.forEach(directory => this.loadDirectory(directory));
|
24 | return this;
|
25 | }
|
26 | loadDirectory(controllerDirectory) {
|
27 | const controllers = fs_1.default.readdirSync(controllerDirectory)
|
28 | .filter((fileName) => !!fileName.match(/^(?!(index)).*[^d]\.(js|ts)$/))
|
29 | .map((fileName) => fileName.replace(/\.(js|ts)$/, ''))
|
30 | .map((controller) => {
|
31 | const Controller = require(path_1.default.resolve(controllerDirectory, controller));
|
32 | if (typeof Controller === 'function') {
|
33 | return Controller;
|
34 | }
|
35 | if (typeof Controller.default === 'function') {
|
36 | return Controller.default;
|
37 | }
|
38 | if (typeof Controller[controller] === 'function') {
|
39 | return Controller[controller];
|
40 | }
|
41 | throw new TypeError(`Unable to load controller "${controller}" due to missing constructable export.`);
|
42 | });
|
43 | this.registerControllers(controllers);
|
44 | }
|
45 | getController(controller) {
|
46 | return this.get(controller);
|
47 | }
|
48 | registerControllers(controllers) {
|
49 | controllers.forEach(Controller => this.registerController(Controller));
|
50 | return this;
|
51 | }
|
52 | registerController(Controller) {
|
53 | this.registerInvokable(Controller, Controller);
|
54 | this.registerAlias(Controller.name, Controller);
|
55 | return this;
|
56 | }
|
57 | }
|
58 | exports.ControllerManager = ControllerManager;
|
59 |
|
\ | No newline at end of file |