UNPKG

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