UNPKG

4.61 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12process.env.SUPPRESS_NO_CONFIG_WARNING = "true";
13const stringify = require("json-stringify-safe");
14const automationClient_1 = require("../lib/automationClient");
15const configuration_1 = require("../lib/configuration");
16const ConsoleMessageClient_1 = require("../lib/internal/message/ConsoleMessageClient");
17const string_1 = require("../lib/internal/util/string");
18const scan_1 = require("../lib/scan");
19main();
20/**
21 * Parse command line CommandInvocation argument, set up, and call the
22 * command handler. This method will not return.
23 */
24function main() {
25 return __awaiter(this, void 0, void 0, function* () {
26 if (!process.argv[2]) {
27 console.error(`[ERROR] Missing command, you must supply the CommandInvocation on the command line`);
28 process.exit(3);
29 }
30 if (process.argv.length > 3) {
31 console.warn(`[WARN] Extra command line arguments will be ignored: ${process.argv.slice(3).join(" ")}`);
32 }
33 const ciString = process.argv[2];
34 try {
35 const ci = JSON.parse(ciString);
36 const configuration = yield configuration_1.loadConfiguration();
37 scan_1.enableDefaultScanning(configuration);
38 const node = automationClient_1.automationClient(configuration);
39 yield invokeOnConsole(node.automationServer, ci, createHandlerContext(configuration));
40 }
41 catch (e) {
42 console.error(`[ERROR] Unhandled exception: ${e.message}`);
43 process.exit(101);
44 }
45 console.error(`[ERROR] Illegal state: unhandled execution path`);
46 process.exit(99);
47 });
48}
49/**
50 * Create a simple handler context for running command handlers from
51 * the command line.
52 */
53function createHandlerContext(config) {
54 return {
55 workspaceId: config.workspaceIds[0],
56 correlationId: string_1.guid(),
57 messageClient: ConsoleMessageClient_1.consoleMessageClient,
58 };
59}
60/**
61 * Run a command handler on the command line. This function will not
62 * return.
63 *
64 * @param automationServer automation server with the command
65 * @param ci command and its parameters
66 * @param ctx suitable execution context
67 */
68function invokeOnConsole(automationServer, ci, ctx) {
69 return __awaiter(this, void 0, void 0, function* () {
70 // Set up the parameter, mappend parameters and secrets
71 const handler = automationServer.automations.commands.find(c => c.name === ci.name);
72 if (!handler) {
73 const commands = automationServer.automations.commands.map(c => c.name).join(" ");
74 console.error(`[ERROR] Unable to find command ${ci.name}, available commands: ${commands}`);
75 process.exit(4);
76 }
77 const invocation = {
78 name: ci.name,
79 args: ci.args ? ci.args.filter(a => handler.parameters.some(p => p.name === a.name)) : undefined,
80 mappedParameters: ci.args ? ci.args.filter(a => handler.mapped_parameters.some(p => p.name === a.name)) : undefined,
81 secrets: ci.args ? ci.args.filter(a => handler.secrets.some(p => p.name === a.name))
82 .map(a => {
83 const s = handler.secrets.find(p => p.name === a.name);
84 return { uri: s.uri, value: a.value };
85 }) : undefined,
86 };
87 try {
88 automationServer.validateCommandInvocation(invocation);
89 }
90 catch (e) {
91 console.error(`[ERROR] Invalid parameters: ${e.message}`);
92 process.exit(2);
93 }
94 try {
95 const result = yield automationServer.invokeCommand(invocation, ctx);
96 console.log(`Command succeeded: ${stringify(result, null, 2)}`);
97 }
98 catch (e) {
99 console.error(`[ERROR] Command failed: ${stringify(e, null, 2)}`);
100 process.exit(1);
101 }
102 process.exit(0);
103 });
104}
105//# sourceMappingURL=command.js.map
\No newline at end of file