UNPKG

trm-client

Version:

TRM (Transport Request Manager) Client

268 lines (267 loc) 10.1 kB
"use strict"; 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 }); exports.alias = alias; const systemAlias_1 = require("../systemAlias"); const createAlias_1 = require("./createAlias"); const deleteAlias_1 = require("./deleteAlias"); const prompts_1 = require("./prompts"); const utils_1 = require("../utils"); const trm_commons_1 = require("trm-commons"); const _create = () => __awaiter(void 0, void 0, void 0, function* () { const inq1 = yield trm_commons_1.Inquirer.prompt({ name: 'name', message: 'Alias name', type: 'input' }); yield (0, createAlias_1.createAlias)({ alias: inq1.name }); }); const _view = (alias) => { const client = alias.login.client; const lang = alias.login.lang; const user = alias.login.user; const hasPassword = alias.login.passwd ? true : false; if (alias.type === utils_1.SystemConnectorType.RFC) { const dest = alias.connection.dest; const ashost = alias.connection.ashost; const sysnr = alias.connection.sysnr; const saprouter = alias.connection.saprouter; const client = alias.login.client; if (dest) { trm_commons_1.Logger.info(`System ID: ${dest}`); } else { trm_commons_1.Logger.warning(`System ID: Unknown`); } if (ashost) { trm_commons_1.Logger.info(`Application server: ${ashost}`); } else { trm_commons_1.Logger.warning(`Application server: Unknown`); } if (sysnr) { trm_commons_1.Logger.info(`Instance number: ${sysnr}`); } else { trm_commons_1.Logger.warning(`Instance number: Unknown`); } if (saprouter) { trm_commons_1.Logger.info(`SAProuter: ${saprouter}`); } if (client) { trm_commons_1.Logger.info(`Logon client: ${client}`); } else { trm_commons_1.Logger.warning(`Logon client: Unknown`); } } else if (alias.type === utils_1.SystemConnectorType.REST) { const endpoint = alias.connection.endpoint; const rfcdest = alias.connection.rfcdest; if (endpoint) { trm_commons_1.Logger.info(`System endpoint: ${endpoint}`); } else { trm_commons_1.Logger.warning(`System endpoint: Unknown`); } if (rfcdest) { trm_commons_1.Logger.info(`RFC Forward: ${rfcdest}`); } } if (lang) { trm_commons_1.Logger.info(`Logon language: ${lang}`); } else { trm_commons_1.Logger.warning(`Logon language: Unknown`); } if (client) { trm_commons_1.Logger.info(`Logon client: ${client}`); } else { trm_commons_1.Logger.warning(`Logon client: Unknown`); } if (user) { trm_commons_1.Logger.info(`Logon user: ${user}`); } else { trm_commons_1.Logger.warning(`Logon user: Unknown`); } if (hasPassword) { trm_commons_1.Logger.info(`Logon password: SAVED IN PLAIN TEXT`); } else { trm_commons_1.Logger.warning(`Logon password: Unknown`); } }; const _check = (alias) => __awaiter(void 0, void 0, void 0, function* () { trm_commons_1.Logger.loading(`Checking connection with alias "${alias.alias}"...`); const oSystemAlias = new systemAlias_1.SystemAlias(alias.type, alias.connection, alias.login); try { yield oSystemAlias.getConnection().connect(); trm_commons_1.Logger.success(`Connection OK.`); } catch (e) { trm_commons_1.Logger.error(`Connection failed!`); trm_commons_1.Logger.error(e.toString()); } }); const _edit = (alias) => __awaiter(void 0, void 0, void 0, function* () { var connectionSuccess = true; const connectionArgs = yield (0, prompts_1.connect)(Object.assign(Object.assign(Object.assign({}, alias.connection), alias.login), { type: alias.type, noSystemAlias: true, force: true }), false); try { systemAlias_1.SystemAlias.delete(alias.alias); var updatedAlias; if (connectionArgs.type === utils_1.SystemConnectorType.RFC) { updatedAlias = systemAlias_1.SystemAlias.create(alias.alias, connectionArgs.type, { ashost: connectionArgs.ashost, dest: connectionArgs.dest, sysnr: connectionArgs.sysnr, saprouter: connectionArgs.saprouter }, { client: connectionArgs.client, lang: connectionArgs.lang, passwd: connectionArgs.passwd, user: connectionArgs.user }); } else if (connectionArgs.type === utils_1.SystemConnectorType.REST) { updatedAlias = systemAlias_1.SystemAlias.create(alias.alias, connectionArgs.type, { endpoint: connectionArgs.endpoint, rfcdest: connectionArgs.forwardRfcDest }, { lang: connectionArgs.lang, passwd: connectionArgs.passwd, user: connectionArgs.user, client: connectionArgs.client }); } yield updatedAlias.getConnection().connect(); } catch (e) { connectionSuccess = false; throw e; } finally { if (connectionSuccess) { trm_commons_1.Logger.success(`Alias "${alias.alias}" updated.`); } else { trm_commons_1.Logger.error(`Alias "${alias.alias}" couldn't be updated.`); systemAlias_1.SystemAlias.delete(alias.alias); if (alias.type === utils_1.SystemConnectorType.RFC) { systemAlias_1.SystemAlias.create(alias.alias, alias.type, { ashost: alias.connection.ashost, dest: alias.connection.dest, sysnr: alias.connection.sysnr, saprouter: alias.connection.saprouter }, { client: alias.login.client, lang: alias.login.lang, passwd: alias.login.passwd, user: alias.login.user }); } else if (alias.type === utils_1.SystemConnectorType.REST) { systemAlias_1.SystemAlias.create(alias.alias, alias.type, { endpoint: alias.connection.endpoint, rfcdest: alias.connection.rfcdest }, { lang: alias.login.lang, passwd: alias.login.passwd, user: alias.login.user, client: alias.login.client }); } } } }); const _delete = (alias) => __awaiter(void 0, void 0, void 0, function* () { yield (0, deleteAlias_1.deleteAlias)({ alias: alias.alias }); }); function alias(commandArgs) { return __awaiter(this, void 0, void 0, function* () { const aAlias = systemAlias_1.SystemAlias.getAll(); var aliasPick = ''; var inq1Choices = []; if (commandArgs.alias) { if (!aAlias.find(o => o.alias === commandArgs.alias)) { trm_commons_1.Logger.warning(`Alias "${commandArgs.alias}" not found.`); inq1Choices.push({ name: `Create alias`, value: `create` }); } else { aliasPick = commandArgs.alias; } } inq1Choices = inq1Choices.concat([{ name: aliasPick ? `View "${aliasPick}"` : `View alias`, value: `pick_view` }, { name: aliasPick ? `Check "${aliasPick}" connection` : `Check alias connection`, value: `pick_check` }, { name: aliasPick ? `Edit "${aliasPick}"` : `Edit alias`, value: `pick_edit` }, { name: aliasPick ? `Delete "${aliasPick}"` : `Delete alias`, value: `pick_delete` }]); const inq1 = yield trm_commons_1.Inquirer.prompt({ name: `action`, message: `Action`, type: `list`, choices: inq1Choices }); if (inq1.action.startsWith(`pick_`) && !aliasPick) { const inq2 = yield trm_commons_1.Inquirer.prompt({ name: `aliasPick`, message: `Select system alias`, type: `list`, choices: aAlias.map(o => { return { name: o.alias, value: o.alias }; }) }); aliasPick = inq2.aliasPick; } const oAliasPick = aAlias.find(o => o.alias === aliasPick); const action = inq1.action.replace(/^pick_/gmi, ''); switch (action) { case 'create': yield _create(); break; case 'view': _view(oAliasPick); break; case 'check': yield _check(oAliasPick); break; case 'edit': yield _edit(oAliasPick); break; case 'delete': yield _delete(oAliasPick); break; } }); }