UNPKG

@ui5/task-adaptation

Version:

Custom task for ui5-builder which allows building UI5 Flexibility Adaptation Projects for SAP BTP, Cloud Foundry environment

122 lines 5.53 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import HTML5RepoManager from "../repositories/html5RepoManager.js"; import { cached } from "../cache/cacheHolder.js"; import { validateObject } from "../util/commonUtil.js"; import CFUtil from "../util/cfUtil.js"; import { getLogger } from "@ui5/logger"; const log = getLogger("@ui5/task-adaptation::CFProcessor"); export default class CFProcessor { configuration; constructor(configuration) { this.configuration = configuration; } async getAppVariantIdHierarchy(appId) { const metadata = await HTML5RepoManager.getMetadata(this.configuration); return [{ repoName: this.configuration.appName, appVariantId: appId, cachebusterToken: metadata.changedOn }]; } fetch(_repoName, _cachebusterToken) { return HTML5RepoManager.getBaseAppFiles(this.configuration); } validateConfiguration() { validateObject(this.configuration, ["appHostId", "appName", "appVersion"], "should be specified in ui5.yaml configuration"); } async updateLandscapeSpecificContent(baseAppManifest, baseAppFiles) { this.updateCloudPlatform(baseAppManifest); await this.updateXsAppJson(baseAppFiles); } async updateXsAppJson(baseAppFiles) { const xsAppJsonContent = baseAppFiles.get("xs-app.json"); if (!xsAppJsonContent) { return; } // Also skip if no routes or no routes with a destination property const xsAppJson = JSON.parse(xsAppJsonContent); if (!Array.isArray(xsAppJson.routes) || !xsAppJson.routes.some((route) => route.destination)) { log.verbose(`No routes with 'destination' found in xs-app.json for app '${this.configuration.appName}'. Skipping xs-app.json update.`); return; } const { serviceInstanceName, space } = this.configuration; if (!serviceInstanceName) { throw new Error(`Service instance name must be specified in ui5.yaml configuration for app '${this.configuration.appName}'`); } let serviceCredentials; try { // Get valid service keys with proper endpoints structure serviceCredentials = await CFUtil.getOrCreateServiceKeyWithEndpoints(serviceInstanceName, space); } catch (error) { throw new Error(`Failed to get valid service keys for app '${this.configuration.appName}': ${error.message}`); } if (serviceCredentials) { xsAppJson.routes = this.enhanceRoutesWithEndpointAndService(serviceCredentials, xsAppJson.routes); baseAppFiles.set("xs-app.json", JSON.stringify(xsAppJson, null, 2)); } else { log.info(`No endpoints found for app '${this.configuration.appName}'. xs-app.json will not be updated.`); } } enhanceRoutesWithEndpointAndService(serviceCredentials, baseRoutes) { const endpoints = serviceCredentials.endpoints; // Map destinations to endpoint names const destinationToEndpoint = Object.entries(endpoints).reduce((acc, [endpointName, obj]) => { if (obj.destination) { acc[obj.destination] = endpointName; } return acc; }, {}); return baseRoutes.map((route) => { const endpointName = destinationToEndpoint[route.destination]; if (endpointName) { // There is a matching endpoint: remove destination and add endpoint/service const { destination: _destination, ...rest } = route; return { ...rest, endpoint: endpointName, service: serviceCredentials["sap.cloud.service"], }; } else { // No match: return route unchanged return route; } }); } updateCloudPlatform(baseAppManifest) { const sapCloudService = baseAppManifest["sap.cloud"]?.service; const sapPlatformCf = baseAppManifest["sap.platform.cf"]; if (sapPlatformCf?.oAuthScopes && sapCloudService) { sapPlatformCf.oAuthScopes = sapPlatformCf.oAuthScopes.map((scope) => scope.replace(`$XSAPPNAME.`, `$XSAPPNAME('${sapCloudService}').`)); } if (this.configuration.sapCloudService) { if (baseAppManifest["sap.cloud"] == null) { baseAppManifest["sap.cloud"] = {}; } baseAppManifest["sap.cloud"].service = this.configuration.sapCloudService; } else { delete baseAppManifest["sap.cloud"]; } } getConfigurationType() { return "cf"; } createAppVariantHierarchyItem(appVariantId, version) { return { appVariantId, version }; } } __decorate([ cached() ], CFProcessor.prototype, "fetch", null); //# sourceMappingURL=cfProcessor.js.map