UNPKG

3.59 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const appRoot = require("app-root-path");
4const fg = require("fast-glob");
5const string_1 = require("./internal/util/string");
6const logger_1 = require("./util/logger");
7class 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}
43const registry = new HandlerRegistry();
44function registerCommand(command) {
45 registry.registerCommand(command);
46}
47exports.registerCommand = registerCommand;
48function registerEvent(event) {
49 registry.registerEvent(event);
50}
51exports.registerEvent = registerEvent;
52/*
53 * Scan the node module/project for command handlers.
54 * Optional glob patterns can be specified to narrow the search.
55 */
56function scanCommands(patterns = ["**/commands/**/*.js"]) {
57 registry.start(true, false);
58 // tslint:disable-next-line:variable-name
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}
65exports.scanCommands = scanCommands;
66/*
67 * Scan the node module/project for event handlers.
68 * Optional glob patterns can be specified to narrow the search.
69 */
70function scanEvents(patterns = ["**/events/**/*.js"]) {
71 registry.start(false, true);
72 // tslint:disable-next-line:variable-name
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}
79exports.scanEvents = scanEvents;
80/*
81 * Enable scanning on the given Configuration instance.
82 */
83function enableDefaultScanning(configuration) {
84 if (!configuration.commands) {
85 configuration.commands = scanCommands();
86 }
87 if (!configuration.events) {
88 configuration.events = scanEvents();
89 }
90 return configuration;
91}
92exports.enableDefaultScanning = enableDefaultScanning;
93function 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}
100function 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//# sourceMappingURL=scan.js.map
\No newline at end of file