UNPKG

trm-client

Version:

TRM (Transport Request Manager) Client

167 lines (166 loc) 6.24 kB
"use strict"; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegistryAlias = 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_core_1 = require("trm-core"); const REGISTRY_FILE_NAME = "registry.ini"; class RegistryAlias { constructor(_endpoint, _name) { this._endpoint = _endpoint; this._name = _name; } setAuthData(data) { if (data && Object.keys(data).length > 0) { this.authData = data; } else { this.authData = null; } return this; } getRegistry() { return new trm_core_1.Registry(this._endpoint, this._name); } static generateFile(content, filePath) { if (!filePath) { filePath = this.getSystemAliasFilePath(); } var oContent = {}; content.forEach(o => { const auth = o.auth; var sAuth; if (typeof (auth) === 'string') { sAuth = auth; } else if (typeof (auth) === "object") { sAuth = JSON.stringify(o.auth); } else { sAuth = null; } oContent[o.alias] = { auth: sAuth }; if (o.endpointUrl) { oContent[o.alias].endpoint = o.endpointUrl; } }); fs.writeFileSync(filePath, ini.encode(oContent), { encoding: 'utf8', flag: 'w' }); } static getSystemAliasFilePath() { const filePath = path_1.default.join((0, utils_1.getRoamingFolder)(), REGISTRY_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 => { aAlias.push({ alias: sAlias, endpointUrl: oIni[sAlias].endpoint, auth: JSON.parse(oIni[sAlias].auth) }); }); return aAlias; } static get(name) { const aAlias = this.getAll(); var alias = aAlias.find(o => o.alias.trim().toUpperCase() === name.trim().toUpperCase()); if (alias) { if (name.trim().toLowerCase() === trm_core_1.PUBLIC_RESERVED_KEYWORD) { alias.endpointUrl = trm_core_1.PUBLIC_RESERVED_KEYWORD; alias.alias = trm_core_1.PUBLIC_RESERVED_KEYWORD; } return new RegistryAlias(alias.endpointUrl, alias.alias).setAuthData(alias.auth); } else { throw new Error(`Registry "${name}" not found.`); } } static create(name, endpointUrl, auth = {}) { 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, endpointUrl: endpointUrl.trim().toLowerCase() === trm_core_1.PUBLIC_RESERVED_KEYWORD ? null : endpointUrl, auth }); this.generateFile(aAlias); } return new RegistryAlias(endpointUrl, name).setAuthData(auth); } static delete(name) { var aAlias = this.getAll(); aAlias = aAlias.filter(o => o.alias.trim().toUpperCase() === name.trim().toUpperCase()); this.generateFile(aAlias); } static update(name, auth = {}) { var aAlias = this.getAll(); const alreadyExists = aAlias.findIndex(o => o.alias.trim().toUpperCase() === name.trim().toUpperCase()); if (alreadyExists < 0) { throw new Error(`Alias doesn't exist, can't update.`); } else { aAlias[alreadyExists].auth = JSON.stringify(auth); this.generateFile(aAlias); } } static generatePublicRegistryAlias() { const allRegistries = this.getAll(); if (!allRegistries.find(o => o.alias.trim().toLowerCase() === trm_core_1.PUBLIC_RESERVED_KEYWORD)) { RegistryAlias.create(trm_core_1.PUBLIC_RESERVED_KEYWORD, trm_core_1.PUBLIC_RESERVED_KEYWORD, null); } } static getTemporaryInstance(endpoint, auth) { return new RegistryAlias(endpoint, endpoint).setAuthData(auth); } } exports.RegistryAlias = RegistryAlias;