@atomist/automation-client
Version:
Atomist API for software low-level client
211 lines (207 loc) • 9.14 kB
JavaScript
;
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 });
const chalk_1 = require("chalk");
const cluster = require("cluster");
const _ = require("lodash");
const util_1 = require("util");
const AutomationEventListener_1 = require("../../server/AutomationEventListener");
const logger_1 = require("../../util/logger");
const info_1 = require("../util/info");
class StartupTimeMessageUatomationEventListener extends AutomationEventListener_1.AutomationEventListenerSupport {
startupSuccessful(client) {
return __awaiter(this, void 0, void 0, function* () {
if (cluster.isMaster || !client.configuration.cluster.enabled) {
const uptime = process.uptime();
logger_1.logger.info(`Atomist automation client startup completed in ${uptime.toFixed(2)}s`);
}
});
}
}
exports.StartupTimeMessageUatomationEventListener = StartupTimeMessageUatomationEventListener;
class StartupMessageAutomationEventListener extends AutomationEventListener_1.AutomationEventListenerSupport {
startupSuccessful(client) {
if (cluster.isMaster || !client.configuration.cluster.enabled) {
return showStartupMessages(client.configuration, client.automations.automations);
}
}
}
exports.StartupMessageAutomationEventListener = StartupMessageAutomationEventListener;
exports.DevModeBannerContributor = () => ({ title: "Development", body: chalk_1.default.green("watch mode enabled") });
exports.LoggingBannerContributor = cfg => {
const loggingConfig = logger_1.clientLoggingConfiguration(_.cloneDeep(cfg));
const rows = [` ${chalk_1.default.gray("Logging")}`];
if (loggingConfig.console.enabled) {
rows.push(` ${chalk_1.default.gray("Console")} ${loggingConfig.console.level}`);
}
if (loggingConfig.file.enabled) {
rows.push(` ${chalk_1.default.gray("File")} ${loggingConfig.file.level} ${chalk_1.default.gray("Location")} ${loggingConfig.file.filename}`);
}
return rows.join("\n");
};
/**
* Build and log startup message, including any user banner
* @param {RegistrationConfirmation} registration
* @param {Automations} automations
*/
function showStartupMessages(configuration, automations) {
return __awaiter(this, void 0, void 0, function* () {
logger_1.logger.info(yield createStartupMessage(configuration, automations));
});
}
exports.showStartupMessages = showStartupMessages;
function createStartupMessage(configuration, automations) {
return __awaiter(this, void 0, void 0, function* () {
const msg = `
${yield header(configuration, automations)}
${firstRow(configuration, automations)}
${secondRow(configuration, automations)}
${thirdRow(configuration, automations)}
${forthRow(configuration, automations)}
${handlers(configuration, automations)}
${contributors(configuration)}
${urls(configuration, automations)}
${footer()}
`;
return msg.replace(/^\s*[\r\n]/gm, "\n");
});
}
exports.createStartupMessage = createStartupMessage;
function toAscii(s) {
return __awaiter(this, void 0, void 0, function* () {
const asciify = require("asciify");
const promisified = util_1.promisify(asciify);
try {
return promisified(s, { font: "ogre" });
}
catch (_a) {
return s;
}
});
}
function header(configuration, automations) {
return __awaiter(this, void 0, void 0, function* () {
let message = configuration.name;
const b = configuration.logging.banner.message;
if (typeof b === "string") {
message = chalk_1.default.green(yield toAscii(b));
}
else if (configuration.logging.banner.enabled === true) {
message = chalk_1.default.green(yield toAscii(message));
}
else if (_.isFunction(b)) {
// It's a function returning a banner object
const banner = b(configuration);
message = chalk_1.default[banner.color](banner.asciify ? yield toAscii(banner.banner) : banner.banner);
}
else if (configuration.logging.banner.enabled === false) {
return;
}
return message;
});
}
function firstRow(configuration, automations) {
const gitInfo = info_1.info(automations);
let c = "";
c += ` ${chalk_1.default.grey("Version")} ${configuration.version}`;
if (gitInfo && gitInfo.git) {
c += ` ${chalk_1.default.grey("Sha")} ${gitInfo.git.sha.slice(0, 7)}`;
c += ` ${chalk_1.default.grey("Repository")} ${gitInfo.git.repository}`;
}
return c;
}
function secondRow(configuration, automations) {
let c = "";
if (automations.groups && automations.groups.length > 0) {
c += ` ${chalk_1.default.grey("Groups")} all`;
}
else {
c += ` ${chalk_1.default.grey(`${automations.team_ids.length > 1 ? "Workspaces" : "Workspace"}`)} ${automations.team_ids.join(", ")}`;
}
c += ` ${chalk_1.default.grey("Policy")} ${automations.policy ? automations.policy : "ephemeral"}`;
c += ` ${chalk_1.default.grey("Cluster")} ${configuration.cluster.enabled ? "enabled" : "disabled"}`;
c += ` ${chalk_1.default.grey("Environment")} ${configuration.environment}`;
return c;
}
function thirdRow(configuration, automations) {
let c = "";
c += ` ${chalk_1.default.grey(automations.commands.length === 1 ? "Command" : "Commands")} ${automations.commands.length}`;
c += ` ${chalk_1.default.grey(automations.events.length === 1 ? "Event" : "Events")} ${automations.events.length}`;
c += ` ${chalk_1.default.grey(automations.ingesters.length === 1 ? "Ingester" : "Ingesters")} ${automations.ingesters.length}`;
return c;
}
function forthRow(configuration, automations) {
let c = "";
if (configuration.ws.session) {
c += ` ${chalk_1.default.grey("Session")} ${configuration.ws.session.jwt}`;
}
return c;
}
function handlers(configuration, automations) {
const events = automations.events
.sort((e1, e2) => e1.name.localeCompare(e2.name))
.map(e => ` ${e.expose === false ? chalk_1.default.gray("[") : " "}${e.name}${e.expose === false ? chalk_1.default.gray("]") : ""} ${e.description ? `(${_.upperFirst(e.description)})` : ""}`);
const commands = automations.commands
.sort((c1, c2) => c1.name.localeCompare(c2.name))
.map(cmd => ` ${cmd.expose === false ? chalk_1.default.gray("[") : " "}${cmd.name}${cmd.expose === false ? chalk_1.default.gray("]") : ""} ${cmd.description ?
`(${_.upperFirst(cmd.description)})` : ""} ${cmd.intent ? chalk_1.default.gray(cmd.intent.join(", ")) : ""}`);
let c = "";
if (commands.length > 0 || events.length > 0) {
c += "\n\n";
}
if (commands.length > 0) {
c += `${chalk_1.default.grey(commands.length === 1 ? " Command" : " Commands")}
${commands.join("\n")}`;
}
if (commands.length > 0 && events.length > 0) {
c += "\n\n";
}
if (events.length > 0) {
c += `${chalk_1.default.grey(events.length === 1 ? " Event" : " Events")}
${events.join("\n")}`;
}
return c;
}
function contributors(configuration) {
let c = "";
const contribs = [exports.LoggingBannerContributor, ...(configuration.logging.banner.contributors || [])];
if (contribs && contribs.length > 0) {
c += contribs.map(cfg => {
const section = cfg(configuration);
if (typeof section === "string") {
return section;
}
else {
const bs = section;
return ` ${chalk_1.default.gray(bs.title)}
${bs.body.split("\n").join("\n ")}`;
}
}).join("\n\n");
}
return c;
}
function urls(configuration, automations) {
if (configuration.ws.enabled) {
const c = automations.team_ids.filter(t => t !== "local").map(t => {
return `
${chalk_1.default.grey("Url")} ${chalk_1.default.underline(`https://app.atomist.com/workspace/${t}/sdms`)}
${chalk_1.default.grey("GraphQL")} ${chalk_1.default.underline(`https://app.atomist.com/workspace/${t}/graphql`)}`;
});
return c.join("\n\n");
}
else {
return "";
}
}
function footer() {
return ` ${chalk_1.default.grey("Docs")} https://docs.atomist.com ${chalk_1.default.grey("Support")} https://join.atomist.com`;
}
//# sourceMappingURL=showStartupMessages.js.map