UNPKG

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