trm-client
Version:
TRM (Transport Request Manager) Client
245 lines (244 loc) • 10.1 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SystemAlias = void 0;
const utils_1 = require("../utils");
const path_1 = __importDefault(require("path"));
const fs = __importStar(require("fs"));
const ini = __importStar(require("ini"));
const trm_commons_1 = require("trm-commons");
const SYSTEM_FILE_NAME = "systems.ini";
class SystemAlias {
constructor(type, _connection, _login) {
this.type = type;
this._connection = _connection;
this._login = _login;
}
getConnection() {
return (0, utils_1.getSystemConnector)(this.type, {
connection: this._connection,
login: this._login
});
}
static generateFile(content, filePath) {
if (!filePath) {
filePath = this.getSystemAliasFilePath();
}
var oContent = {};
content.forEach(o => {
if (o.type === utils_1.SystemConnectorType.RFC) {
oContent[o.alias] = {
type: o.type,
dest: o.connection.dest,
ashost: o.connection.ashost,
sysnr: o.connection.sysnr,
client: o.login.client,
user: o.login.user,
passwd: o.login.passwd,
lang: o.login.lang
};
if (o.connection.saprouter) {
oContent[o.alias].saprouter = o.connection.saprouter;
}
}
else if (o.type === utils_1.SystemConnectorType.REST) {
oContent[o.alias] = {
type: o.type,
endpoint: o.connection.endpoint,
rfcdest: o.connection.rfcdest || 'NONE',
client: o.login.client,
user: o.login.user,
passwd: o.login.passwd,
lang: o.login.lang
};
}
});
fs.writeFileSync(filePath, ini.encode(oContent), { encoding: 'utf8', flag: 'w' });
}
static getSystemAliasFilePath() {
const filePath = path_1.default.join((0, utils_1.getRoamingFolder)(), SYSTEM_FILE_NAME);
if (!fs.existsSync(filePath)) {
this.generateFile([], filePath);
}
return filePath;
}
static getAll() {
var aAlias = [];
const filePath = this.getSystemAliasFilePath();
const sIni = fs.readFileSync(filePath).toString();
const oIni = ini.decode(sIni);
Object.keys(oIni).forEach(sAlias => {
if (oIni[sAlias].type === utils_1.SystemConnectorType.RFC || !oIni[sAlias].type) {
aAlias.push({
alias: sAlias,
type: utils_1.SystemConnectorType.RFC,
connection: {
dest: oIni[sAlias].dest,
ashost: oIni[sAlias].ashost,
sysnr: oIni[sAlias].sysnr,
saprouter: oIni[sAlias].saprouter
},
login: {
client: oIni[sAlias].client,
user: oIni[sAlias].user,
passwd: oIni[sAlias].passwd,
lang: oIni[sAlias].lang
}
});
}
else if (oIni[sAlias].type === utils_1.SystemConnectorType.REST) {
aAlias.push({
alias: sAlias,
type: utils_1.SystemConnectorType.REST,
connection: {
endpoint: oIni[sAlias].endpoint,
rfcdest: oIni[sAlias].rfcdest || 'NONE'
},
login: {
user: oIni[sAlias].user,
passwd: oIni[sAlias].passwd,
lang: oIni[sAlias].lang,
client: oIni[sAlias].client
}
});
}
});
return aAlias;
}
static get(name) {
const aAlias = this.getAll();
const alias = aAlias.find(o => o.alias.trim().toUpperCase() === name.trim().toUpperCase());
if (alias) {
return new SystemAlias(alias.type, alias.connection, alias.login);
}
else {
throw new Error(`System alias "${name}" not found.`);
}
}
static create(name, type, connection, login) {
if (!name) {
throw new Error(`Invalid alias name.`);
}
var aAlias = this.getAll();
const alreadyExists = aAlias.find(o => o.alias.trim().toUpperCase() === name.trim().toUpperCase()) ? true : false;
if (alreadyExists) {
throw new Error(`Alias already exists. Choose an unique name.`);
}
else {
aAlias.push({
alias: name,
type,
connection,
login
});
this.generateFile(aAlias);
}
return new SystemAlias(type, connection, login);
}
static delete(name) {
var aAlias = this.getAll();
aAlias = aAlias.filter(o => o.alias.trim().toUpperCase() !== name.trim().toUpperCase());
this.generateFile(aAlias);
}
static createIfNotExists(connArgs) {
return __awaiter(this, void 0, void 0, function* () {
const aAlias = this.getAll();
var alias;
if (connArgs.type === utils_1.SystemConnectorType.RFC) {
alias = aAlias.find(o => {
if (o.type === utils_1.SystemConnectorType.RFC || !o.type) {
const rfcConn = o.connection;
if (rfcConn.dest === connArgs.dest &&
rfcConn.ashost === connArgs.ashost &&
rfcConn.sysnr === connArgs.sysnr &&
rfcConn.saprouter === connArgs.saprouter &&
o.login.client === connArgs.client &&
o.login.lang === connArgs.lang &&
o.login.user === connArgs.user &&
o.login.passwd === connArgs.passwd) {
return o;
}
}
});
}
else if (connArgs.type === utils_1.SystemConnectorType.REST) {
alias = aAlias.find(o => {
if (o.type === utils_1.SystemConnectorType.REST) {
const restConn = o.connection;
if (restConn.endpoint === connArgs.endpoint &&
restConn.rfcdest === connArgs.forwardRfcDest &&
o.login.client === connArgs.client &&
o.login.lang === connArgs.lang &&
o.login.user === connArgs.user &&
o.login.passwd === connArgs.passwd) {
return o;
}
}
});
}
if (!alias) {
const aliasName = (yield trm_commons_1.Inquirer.prompt([{
name: 'create',
message: 'Create new alias for connection?',
type: 'confirm',
default: true
}, {
name: 'alias',
message: 'Alias name',
type: 'input',
when: (hash) => {
return hash.create;
}
}])).alias;
if (aliasName) {
this.create(aliasName, connArgs.type, connArgs, connArgs);
}
}
});
}
}
exports.SystemAlias = SystemAlias;