1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const appRoot = require("app-root-path");
|
4 | const fg = require("fast-glob");
|
5 | const string_1 = require("./internal/util/string");
|
6 | const logger_1 = require("./util/logger");
|
7 | class HandlerRegistry {
|
8 | constructor() {
|
9 | this.commands = [];
|
10 | this.events = [];
|
11 | this.scanForCommands = false;
|
12 | this.scanForEvents = false;
|
13 | }
|
14 | registerCommand(command) {
|
15 | if (this.scanForCommands) {
|
16 | logger_1.logger.debug(`Registered command '${command.name}'`);
|
17 | if (typeof command === "function") {
|
18 | this.commands.push(command);
|
19 | }
|
20 | else {
|
21 | this.commands.push(() => Object.create(command));
|
22 | }
|
23 | }
|
24 | }
|
25 | registerEvent(event) {
|
26 | if (this.scanForEvents) {
|
27 | logger_1.logger.debug(`Registered event '${event.name}'`);
|
28 | if (typeof event === "function") {
|
29 | this.events.push(event);
|
30 | }
|
31 | else {
|
32 | this.events.push(() => Object.create(event));
|
33 | }
|
34 | }
|
35 | }
|
36 | start(commands, events) {
|
37 | this.commands = [];
|
38 | this.scanForCommands = commands;
|
39 | this.events = [];
|
40 | this.scanForEvents = events;
|
41 | }
|
42 | }
|
43 | const registry = new HandlerRegistry();
|
44 | function registerCommand(command) {
|
45 | registry.registerCommand(command);
|
46 | }
|
47 | exports.registerCommand = registerCommand;
|
48 | function registerEvent(event) {
|
49 | registry.registerEvent(event);
|
50 | }
|
51 | exports.registerEvent = registerEvent;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function scanCommands(patterns = ["**/commands/**/*.js"]) {
|
57 | registry.start(true, false);
|
58 |
|
59 | const _patterns = string_1.toStringArray(patterns);
|
60 | logger_1.logger.debug(`Scanning for commands using file patterns: ${_patterns.join(", ")}`);
|
61 | scan(_patterns);
|
62 | logger_1.logger.debug(`Completed scanning for commands`);
|
63 | return registry.commands;
|
64 | }
|
65 | exports.scanCommands = scanCommands;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function scanEvents(patterns = ["**/events/**/*.js"]) {
|
71 | registry.start(false, true);
|
72 |
|
73 | const _patterns = string_1.toStringArray(patterns);
|
74 | logger_1.logger.debug(`Scanning for events using file patterns: ${_patterns.join(", ")}`);
|
75 | scan(_patterns);
|
76 | logger_1.logger.debug(`Completed scanning for events`);
|
77 | return registry.events;
|
78 | }
|
79 | exports.scanEvents = scanEvents;
|
80 |
|
81 |
|
82 |
|
83 | function enableDefaultScanning(configuration) {
|
84 | if (!configuration.commands) {
|
85 | configuration.commands = scanCommands();
|
86 | }
|
87 | if (!configuration.events) {
|
88 | configuration.events = scanEvents();
|
89 | }
|
90 | return configuration;
|
91 | }
|
92 | exports.enableDefaultScanning = enableDefaultScanning;
|
93 | function scan(patterns) {
|
94 | const ignore = ["**/node_modules/**", "**/.git/**", "**/*Test.js", "**/*Tests.js"];
|
95 | patterns.forEach(pattern => {
|
96 | const files = fg.sync(pattern, { ignore });
|
97 | files.forEach(safeRequire);
|
98 | });
|
99 | }
|
100 | function safeRequire(file) {
|
101 | try {
|
102 | logger_1.logger.debug(`Scanning file '${file}'`);
|
103 | require(`${appRoot.path}/${file}`);
|
104 | }
|
105 | catch (err) {
|
106 | logger_1.logger.warn(`Can't require '${file}': ${err.message}`);
|
107 | }
|
108 | }
|
109 |
|
\ | No newline at end of file |