UNPKG

@sap/adp-abap

Version:

abap service for all yeoman generators

148 lines 6.58 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.ManifestManager = void 0; const adp_common_1 = require("@sap/adp-common"); /** * Service class for handling operations related to application manifests. */ class ManifestManager { /** * Creates an instance of ManifestManager. * * @param {AbapProvider} provider - The ABAP provider service. */ constructor(provider) { this.provider = provider; this.manifestCache = new Map(); } /** * Resets the manifest cache. */ resetCache() { this.manifestCache = new Map(); } /** * Retrieves the cached manifest for a specified application. * * @param {string} id - The ID of the application whose manifest is needed. * @returns {Promise<Manifest | undefined>} The cached manifest or null if not available. */ getManifest(id) { var _a; return __awaiter(this, void 0, void 0, function* () { if (!id) { return undefined; } yield this.loadManifest(id); return (_a = this.manifestCache.get(id)) === null || _a === void 0 ? void 0 : _a.manifest; }); } /** * Retrieves the cached manifest URL for a specified application. * * @param {string} id - The ID of the application whose manifest URL is needed. * @returns {Promise<string | undefined>} The cached URL or an empty string if not available. */ getUrl(id) { var _a; return __awaiter(this, void 0, void 0, function* () { if (!id) { return undefined; } yield this.loadManifestUrl(id); return (_a = this.manifestCache.get(id)) === null || _a === void 0 ? void 0 : _a.url; }); } /** * Retrieves and caches the manifest URL and the manifest itself for a specific application. * Uses caching to avoid redundant network requests. * * @param {string} id - The ID of the application for which to load the manifest. * @returns {Promise<void>} The manifest URL. */ loadManifestUrl(id) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const cached = this.manifestCache.get(id); if (cached === null || cached === void 0 ? void 0 : cached.url) { return; } const provider = this.provider.getProvider(); const appIndex = provider.getAppIndex(); const data = yield appIndex.getAppInfo(id); if (data) { const appInfo = Object.values(data)[0]; const url = (_b = (_a = appInfo === null || appInfo === void 0 ? void 0 : appInfo.manifestUrl) !== null && _a !== void 0 ? _a : appInfo === null || appInfo === void 0 ? void 0 : appInfo.manifest) !== null && _b !== void 0 ? _b : ""; this.manifestCache.set(id, { url, manifest: undefined }); } }); } /** * Fetches and stores the application manifest from a URL. * * @param {string} id - The application ID. * @returns {Promise<Manifest>} The fetched manifest. */ loadManifest(id) { return __awaiter(this, void 0, void 0, function* () { const provider = this.provider.getProvider(); let cached = this.manifestCache.get(id); if (cached === null || cached === void 0 ? void 0 : cached.manifest) { return; } if (!(cached === null || cached === void 0 ? void 0 : cached.url)) { yield this.loadManifestUrl(id); cached = this.manifestCache.get(id); } if (!(cached === null || cached === void 0 ? void 0 : cached.url)) { throw new Error("Manifest URL could not be loaded."); } try { const response = yield provider.request({ url: cached.url }); const manifest = JSON.parse(response.data); if (typeof manifest !== "object" || manifest === null) { throw new Error("Manifest parsing error. Manifest is not in expected format."); } this.manifestCache.set(id, { url: cached.url, manifest }); } catch (e) { adp_common_1.Logger.getLogger.error(`Failed to load manifest, error: ${e.message}`); throw new Error(`Failed to load manifest from URL: ${e.message}`); } }); } /** * Determines if the application supports manifest-first approach and manifest url exists. * * @param {string} id - The application ID. * @returns {Promise<boolean>} True if supported, otherwise throws an error. */ isAppSupported(id) { return __awaiter(this, void 0, void 0, function* () { const provider = this.provider.getProvider(); const appIndex = provider.getAppIndex(); const isSupported = yield appIndex.getIsManiFirstSupported(id); if (!isSupported) { adp_common_1.Logger.getLogger.error(`Application '${id}' is not supported by Adaptation Project`); throw new Error("Select a different application. The selected application is not supported by Adaptation Project. Please refer to SAPUI5 Adaptation Project documentation for more information."); } const url = yield this.getUrl(id); if (!url) { adp_common_1.Logger.getLogger.error(`Manifest url for app '${id}' was not found!`); throw new Error("Select a different application. Adaptation project doesn't support the selected application."); } return true; }); } } exports.ManifestManager = ManifestManager; //# sourceMappingURL=ManifestManager.js.map