@atomist/automation-client
Version:
Atomist API for software low-level client
105 lines • 5.53 kB
JavaScript
;
/*
* Copyright © 2018 Atomist, Inc.
*
* See LICENSE file.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
// tslint:disable-next-line:no-import-side-effect
require("source-map-support/register");
const appRoot = require("app-root-path");
const _ = require("lodash");
const path = require("path");
const error_1 = require("../lib/util/error");
function main() {
return __awaiter(this, void 0, void 0, function* () {
try {
const logging = require("../lib/util/logger");
logging.configureLogging(logging.ClientLogging);
let cfg = yield require("../lib/configuration").loadConfiguration();
cfg = require("../lib/scan").enableDefaultScanning(cfg);
cfg.logging.banner.contributors.push(require("../lib/internal/transport/showStartupMessages").DevModeBannerContributor);
const automationClient = require("../lib/automationClient").automationClient(cfg);
yield automationClient.run();
const registration = prepareRegistration(cfg);
const chokidar = require("chokidar");
const additionalFilesToWatch = (process.env.ATOMIST_WATCH_PATHS || "").split(",");
const watcher = chokidar.watch(["index.js", "lib/*.js", "lib/**/*.js", ...additionalFilesToWatch], { ignored: "\.ts" });
const indexPath = path.join(appRoot.path, "index.js");
const libPath = path.join(appRoot.path, "lib");
const logger = logging.logger;
watcher.on("ready", () => {
watcher.on("all", (e, filePath) => __awaiter(this, void 0, void 0, function* () {
const start = Date.now();
logger.warn("Change to '%s' file detected. Attempting reload...", filePath);
Object.keys(require.cache).forEach(id => {
if (id.startsWith(indexPath) || id.startsWith(libPath)) {
delete require.cache[id];
}
});
try {
let newCfg = yield require("../lib/configuration").loadConfiguration();
newCfg = require("../lib/scan").enableDefaultScanning(newCfg);
diffRegistration(prepareRegistration(newCfg), registration);
// Clean out previous handlers and install new ones
automationClient.automationServer.commandHandlers = [];
newCfg.commands.forEach(command => automationClient.withCommandHandler(command));
automationClient.automationServer.eventHandlers = [];
newCfg.events.forEach(event => automationClient.withEventHandler(event));
// Now drop reference to previous configuration
automationClient.configuration = newCfg;
// Clean out the startup banner listeners
if (automationClient.defaultListeners.length > 2) {
automationClient.defaultListeners.splice(2);
}
yield automationClient.raiseStartupEvent();
logger.warn(`Reload successful in ${((Date.now() - start) / 1000).toFixed(2)}s`);
}
catch (e) {
logger.error("Reload failed");
error_1.printError(e);
}
}));
});
}
catch (e) {
error_1.printError(e);
process.exit(5);
}
});
}
function prepareRegistration(configuration) {
const automations = new (require("../lib/server/BuildableAutomationServer").BuildableAutomationServer)(configuration);
(configuration.commands || []).forEach(c => automations.registerCommandHandler(c));
(configuration.events || []).forEach(e => automations.registerEventHandler(e));
(configuration.ingesters || []).forEach(i => automations.registerIngester(i));
return require("../lib/internal/transport/websocket/payloads")
.prepareRegistration(automations.automations, {}, configuration.metadata);
}
function diffRegistration(newReg, oldReg) {
if (!_.isEqual(newReg, oldReg)) {
const jsonDiff = require("json-diff");
const logging = require("../lib/util/logger");
logging.logger.error(`Unable to reload. Incompatible changes to registration metadata detected:
${jsonDiff.diffString(newReg, oldReg).trim()}`);
logging.logger.error("Exiting...");
process.exit(15);
}
}
/* tslint:disable:no-console */
main()
.catch(e => {
console.error(`Unhandled exception: ${e.message}`);
process.exit(10);
});
//# sourceMappingURL=start-dev.js.map