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