"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.selfDescribeCommand = void 0;
const globals_1 = require("@atomist/automation-client/lib/globals");
const HandlerResult_1 = require("@atomist/automation-client/lib/HandlerResult");
const info_1 = require("@atomist/automation-client/lib/internal/util/info");
const slack_messages_1 = require("@atomist/slack-messages");
const appRoot = require("app-root-path");
const path = require("path");
/**
 * Return a command handler that can describe the present software delivery machine
 * @param {SoftwareDeliveryMachine} sdm
 * @return {HandleCommand<EmptyParameters>}
 */
function selfDescribeCommand(sdm) {
    return {
        name: "SelfDescribe",
        listener: selfDescribeListener(sdm),
        description: "Describe this SDM",
        intent: [`describe sdm ${sdm.configuration.name.replace("@", "")}`],
    };
}
exports.selfDescribeCommand = selfDescribeCommand;
function selfDescribeListener(sdm) {
    return async (cli) => {
        const pj = require(path.join(appRoot.path, "package.json"));
        const clientPj = require(path.join(appRoot.path, "node_modules", "@atomist", "automation-client", "package.json"));
        const sdmPj = require(path.join(appRoot.path, "node_modules", "@atomist", "sdm", "package.json"));
        const gitInfo = info_1.info(globals_1.automationClientInstance().automations.automations);
        const msg = {
            attachments: [{
                    author_name: pj.author && pj.author.name ? pj.author.name : pj.author,
                    title: sdm.name,
                    title_link: pj.homepage,
                    fallback: sdm.name,
                    text: `${pj.description}
Version: ${slack_messages_1.codeLine(sdm.configuration.version)} - License: ${slack_messages_1.codeLine(pj.license)}`,
                }, {
                    author_name: "Details",
                    fallback: "Details",
                    text: `Sha: ${slack_messages_1.codeLine(gitInfo.git && gitInfo.git.sha ? gitInfo.git.sha.slice(0, 7) : "n/a")}
Repository: ${slack_messages_1.codeLine(gitInfo.git && gitInfo.git.repository ? gitInfo.git.repository : "n/a")}
Policy: ${slack_messages_1.codeLine(sdm.configuration.policy)}
Environment: ${slack_messages_1.codeLine(sdm.configuration.environment)}
Cluster: ${slack_messages_1.codeLine(sdm.configuration.cluster.enabled ? "enabled" : "disabled")}`,
                }, {
                    author_name: "Dependencies",
                    fallback: "Dependencies",
                    text: `${slack_messages_1.codeLine(`${clientPj.name}:${clientPj.version}`)}
${slack_messages_1.codeLine(`${sdmPj.name}:${sdmPj.version}`)}`,
                }, {
                    author_name: "Extension Packs",
                    fallback: "Extension Packs",
                    text: [...sdm.extensionPacks]
                        .sort((e1, e2) => e1.name.localeCompare(e2.name))
                        .map(e => `${slack_messages_1.codeLine(`${e.name}:${e.version}`)} ${e.vendor}`).join("\n"),
                }, {
                    author_name: "Events",
                    fallback: "Events",
                    text: globals_1.automationClientInstance().automations.automations.events
                        .sort((e1, e2) => e1.name.localeCompare(e2.name))
                        .map(e => `${slack_messages_1.bold(e.name)} ${e.description}`).join("\n"),
                }, {
                    author_name: "Commands",
                    fallback: "Commands",
                    text: globals_1.automationClientInstance().automations.automations.commands
                        .sort((e1, e2) => e1.name.localeCompare(e2.name))
                        .map(e => `${slack_messages_1.bold(e.name)} ${e.intent.map(slack_messages_1.codeLine).join(", ")} ${e.description}`).join("\n"),
                    footer: `${sdm.configuration.name}:${sdm.configuration.version}`,
                }],
        };
        await cli.context.messageClient.respond(msg);
        return HandlerResult_1.Success;
    };
}
//# sourceMappingURL=SelfDescribe.js.map