1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
9 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
10 | return new (P || (P = Promise))(function (resolve, reject) {
|
11 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
12 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
13 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
14 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
15 | });
|
16 | };
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 |
|
19 | require("source-map-support/register");
|
20 | const appRoot = require("app-root-path");
|
21 | const _ = require("lodash");
|
22 | const path = require("path");
|
23 | const error_1 = require("../lib/util/error");
|
24 | function main() {
|
25 | return __awaiter(this, void 0, void 0, function* () {
|
26 | try {
|
27 | const logging = require("../lib/util/logger");
|
28 | logging.configureLogging(logging.ClientLogging);
|
29 | let cfg = yield require("../lib/configuration").loadConfiguration();
|
30 | cfg = require("../lib/scan").enableDefaultScanning(cfg);
|
31 | cfg.logging.banner.contributors.push(require("../lib/internal/transport/showStartupMessages").DevModeBannerContributor);
|
32 | const automationClient = require("../lib/automationClient").automationClient(cfg);
|
33 | yield automationClient.run();
|
34 | const registration = prepareRegistration(cfg);
|
35 | const chokidar = require("chokidar");
|
36 | const watcher = chokidar.watch(["index.js", "lib/*.js", "lib/**/*.js"], { ignored: "\.ts" });
|
37 | const indexPath = path.join(appRoot.path, "index.js");
|
38 | const libPath = path.join(appRoot.path, "lib");
|
39 | const logger = logging.logger;
|
40 | watcher.on("ready", () => {
|
41 | watcher.on("all", (e, filePath) => __awaiter(this, void 0, void 0, function* () {
|
42 | const start = Date.now();
|
43 | logger.warn("Change to '%s' file detected. Attempting reload...", filePath);
|
44 | Object.keys(require.cache).forEach(id => {
|
45 | if (id.startsWith(indexPath) || id.startsWith(libPath)) {
|
46 | delete require.cache[id];
|
47 | }
|
48 | });
|
49 | try {
|
50 | let newCfg = yield require("../lib/configuration").loadConfiguration();
|
51 | newCfg = require("../lib/scan").enableDefaultScanning(newCfg);
|
52 | diffRegistration(prepareRegistration(newCfg), registration);
|
53 |
|
54 | automationClient.automationServer.commandHandlers = [];
|
55 | newCfg.commands.forEach(command => automationClient.withCommandHandler(command));
|
56 | automationClient.automationServer.eventHandlers = [];
|
57 | newCfg.events.forEach(event => automationClient.withEventHandler(event));
|
58 |
|
59 | automationClient.configuration = newCfg;
|
60 |
|
61 | if (automationClient.defaultListeners.length > 2) {
|
62 | automationClient.defaultListeners.splice(2);
|
63 | }
|
64 | yield automationClient.raiseStartupEvent();
|
65 | logger.warn(`Reload successful in ${((Date.now() - start) / 1000).toFixed(2)}s`);
|
66 | }
|
67 | catch (e) {
|
68 | logger.error("Reload failed");
|
69 | error_1.printError(e);
|
70 | }
|
71 | }));
|
72 | });
|
73 | }
|
74 | catch (e) {
|
75 | error_1.printError(e);
|
76 | process.exit(5);
|
77 | }
|
78 | });
|
79 | }
|
80 | function prepareRegistration(configuration) {
|
81 | const automations = new (require("../lib/server/BuildableAutomationServer").BuildableAutomationServer)(configuration);
|
82 | (configuration.commands || []).forEach(c => automations.registerCommandHandler(c));
|
83 | (configuration.events || []).forEach(e => automations.registerEventHandler(e));
|
84 | (configuration.ingesters || []).forEach(i => automations.registerIngester(i));
|
85 | return require("../lib/internal/transport/websocket/payloads")
|
86 | .prepareRegistration(automations.automations, {}, configuration.metadata);
|
87 | }
|
88 | function diffRegistration(newReg, oldReg) {
|
89 | if (!_.isEqual(newReg, oldReg)) {
|
90 | const jsonDiff = require("json-diff");
|
91 | const logging = require("../lib/util/logger");
|
92 | logging.logger.error(`Unable to reload. Incompatible changes to registration metadata detected:
|
93 | ${jsonDiff.diffString(newReg, oldReg).trim()}`);
|
94 | logging.logger.error("Exiting...");
|
95 | process.exit(15);
|
96 | }
|
97 | }
|
98 |
|
99 | main()
|
100 | .catch(e => {
|
101 | console.error(`Unhandled exception: ${e.message}`);
|
102 | process.exit(10);
|
103 | });
|
104 |
|
\ | No newline at end of file |