UNPKG

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