@atomist/automation-client
Version:
Atomist API for software low-level client
116 lines • 4.91 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 });
process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
const stringify = require("json-stringify-safe");
const automationClient_1 = require("../lib/automationClient");
const configuration_1 = require("../lib/configuration");
const ConsoleMessageClient_1 = require("../lib/internal/message/ConsoleMessageClient");
const string_1 = require("../lib/internal/util/string");
const scan_1 = require("../lib/scan");
/* tslint:disable:no-console */
main()
.catch((err) => {
console.error(`Unhandled exception: ${err.message}`);
process.exit(101);
});
/**
* Parse command line CommandInvocation argument, set up, and call the
* command handler. This method will not return.
*/
function main() {
return __awaiter(this, void 0, void 0, function* () {
if (!process.argv[2]) {
console.error(`[ERROR] Missing command, you must supply the CommandInvocation on the command line`);
process.exit(3);
}
if (process.argv.length > 3) {
console.warn(`[WARN] Extra command line arguments will be ignored: ${process.argv.slice(3).join(" ")}`);
}
const ciString = process.argv[2];
try {
const ci = JSON.parse(ciString);
const configuration = yield configuration_1.loadConfiguration();
scan_1.enableDefaultScanning(configuration);
const node = automationClient_1.automationClient(configuration);
yield invokeOnConsole(node.automationServer, ci, createHandlerContext(configuration));
}
catch (e) {
console.error(`[ERROR] Unhandled exception: ${e.message}`);
process.exit(101);
}
console.error(`[ERROR] Illegal state: unhandled execution path`);
process.exit(99);
});
}
/**
* Create a simple handler context for running command handlers from
* the command line.
*/
function createHandlerContext(config) {
return {
workspaceId: config.workspaceIds[0],
correlationId: string_1.guid(),
messageClient: ConsoleMessageClient_1.consoleMessageClient,
};
}
/**
* Run a command handler on the command line. This function will not
* return.
*
* @param automationServer automation server with the command
* @param ci command and its parameters
* @param ctx suitable execution context
*/
function invokeOnConsole(automationServer, ci, ctx) {
return __awaiter(this, void 0, void 0, function* () {
// Set up the parameter, mappend parameters and secrets
const handler = automationServer.automations.commands.find(c => c.name === ci.name);
if (!handler) {
const commands = automationServer.automations.commands.map(c => c.name).join(" ");
console.error(`[ERROR] Unable to find command ${ci.name}, available commands: ${commands}`);
process.exit(4);
}
const invocation = {
name: ci.name,
args: ci.args ? ci.args.filter(a => handler.parameters.some(p => p.name === a.name)) : undefined,
mappedParameters: ci.args ? ci.args.filter(a => handler.mapped_parameters.some(p => p.name === a.name)) : undefined,
secrets: ci.args ? ci.args.filter(a => handler.secrets.some(p => p.name === a.name))
.map(a => {
const s = handler.secrets.find(p => p.name === a.name);
return { uri: s.uri, value: String(a.value) };
}) : undefined,
};
try {
automationServer.validateCommandInvocation(invocation);
}
catch (e) {
console.error(`[ERROR] Invalid parameters: ${e.message}`);
process.exit(2);
}
try {
const result = yield automationServer.invokeCommand(invocation, ctx);
console.log(`Command succeeded: ${stringify(result, undefined, 2)}`);
}
catch (e) {
console.error(`[ERROR] Command failed: ${stringify(e, undefined, 2)}`);
process.exit(1);
}
process.exit(0);
});
}
//# sourceMappingURL=command.js.map