UNPKG

@sap/adp-cf

Version:
394 lines 18.3 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __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 }); const fs_1 = __importDefault(require("fs")); const path = __importStar(require("path")); const js_yaml_1 = __importDefault(require("js-yaml")); const lodash_1 = __importDefault(require("lodash")); const _1 = require("./"); class YamlUtils { static isMtaProject(selectedPath) { return fs_1.default.existsSync(path.join(selectedPath, this.MTA_YAML_FILE)); } static loadYamlContent(file) { const parsed = this.parseMtaFile(file); this.yamlContent = parsed; this.yamlPath = file; } static adjustMtaYaml(projectPath, moduleName, appRouterType, businessSolutionName, businessService) { return __awaiter(this, void 0, void 0, function* () { this.setTimestamp(); const defaultYaml = { ID: projectPath.split(path.sep).pop(), version: "0.0.1", modules: [], resources: [], "_schema-version": "3.2" }; if (!appRouterType) { appRouterType = this.getRouterType(); } const yamlContent = Object.assign(defaultYaml, this.yamlContent); const projectName = yamlContent.ID.toLowerCase(); const businesServices = yamlContent.resources.map((resource) => resource.name); const initialServices = yamlContent.resources.map((resource) => resource.parameters.service); if (appRouterType === this.STANDALONE_APPROUTER) { this.adjustMtaYamlStandaloneApprouter(yamlContent, projectName, businesServices, businessService); } else if (appRouterType === this.APPROUTER_MANAGED) { this.adjustMtaYamlManagedApprouter(yamlContent, projectName, businessSolutionName, businessService); } this.adjustMtaYamlUDeployer(yamlContent, projectName, moduleName); this.adjustMtaYamlResources(yamlContent, projectName, appRouterType === this.APPROUTER_MANAGED); this.adjustMtaYamlOwnModule(yamlContent, moduleName); // should go last since it sorts the modules (workaround, should be removed after fixed in deployment module) this.adjustMtaYamlFlpModule(yamlContent, projectName, businessService); const updatedYamlContent = js_yaml_1.default.dump(yamlContent); this.createServices(yamlContent.resources, initialServices); return fs_1.default.writeFile(this.yamlPath, updatedYamlContent, "utf-8", this.writeFileCallback); }); } static getRouterType() { const filterd = this.yamlContent.modules.filter((module) => module.name.includes("destination-content") || module.name.includes("approuter")); const routerType = filterd.pop(); if (routerType.name.includes("approuter")) { return this.STANDALONE_APPROUTER; } else { return this.APPROUTER_MANAGED; } } static getProjectName() { return this.yamlContent.ID; } static getProjectNameForXsSecurity() { return `${this.getProjectName().toLowerCase().replace(/\./g, "_")}_${this.timestamp}`; } static setTimestamp() { this.timestamp = Date.now().toString(); } static getSAPCloudService() { const modules = this.yamlContent.modules.filter((module) => module.name.includes("destination-content")); const destinations = modules[0].parameters.content.instance.destinations; let sapCloudService = destinations.find((destination) => destination.Name.includes("html_repo_host")); sapCloudService = sapCloudService["sap.cloud.service"].replace(/_/g, "."); return sapCloudService; } static getAppParamsFromUI5Yaml(projectPath) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const ui5YamlPath = path.join(projectPath, this.UI5_YAML_FILE); const parsedMtaFile = this.parseMtaFile(ui5YamlPath); const appConfiguration = (_b = (_a = parsedMtaFile === null || parsedMtaFile === void 0 ? void 0 : parsedMtaFile.builder) === null || _a === void 0 ? void 0 : _a.customTasks[0]) === null || _b === void 0 ? void 0 : _b.configuration; const appParams = { appHostId: appConfiguration === null || appConfiguration === void 0 ? void 0 : appConfiguration.appHostId, appName: appConfiguration === null || appConfiguration === void 0 ? void 0 : appConfiguration.appName, appVersion: appConfiguration === null || appConfiguration === void 0 ? void 0 : appConfiguration.appVersion, spaceGuid: appConfiguration === null || appConfiguration === void 0 ? void 0 : appConfiguration.space }; return Promise.resolve(appParams); }); } static parseMtaFile(file) { if (!fs_1.default.existsSync(file)) { throw new Error(`Could not find file ${file}`); } const content = fs_1.default.readFileSync(file, "utf-8"); let parsed; try { parsed = js_yaml_1.default.load(content); return parsed; } catch (e) { throw new Error(`Error parsing file ${file}`); } } static createServices(resources, initialServices) { const excludeServices = initialServices.concat(["portal", this.HTML5_APPS_REPO]); const xsSecurityPath = this.yamlPath.replace("mta.yaml", "xs-security.json"); resources.forEach((resource) => { if (!excludeServices.includes(resource.parameters.service)) { if (resource.parameters.service === "xsuaa") { _1.CFUtils.createService(this.spaceGuid, resource.parameters["service-plan"], resource.parameters["service-name"], [], xsSecurityPath, resource.parameters.service); } else { _1.CFUtils.createService(this.spaceGuid, resource.parameters["service-plan"], resource.parameters["service-name"], [], "", resource.parameters.service); } } }); } static writeFileCallback(error) { if (error) { throw new Error("Cannot save mta.yaml file."); } } static adjustMtaYamlStandaloneApprouter(yamlContent, projectName, resourceNames, businessService) { const appRouterName = `${projectName}-approuter`; let appRouter = yamlContent.modules.find((module) => module.name === appRouterName); if (appRouter == null) { appRouter = { name: appRouterName, type: "approuter.nodejs", path: appRouterName, requires: [], parameters: { "disk-quota": "256M", "memory": "256M" } }; yamlContent.modules.push(appRouter); } const requires = [`${projectName}_html_repo_runtime`, `${projectName}_uaa`, `portal_resources_${projectName}`].concat(businessService); requires.forEach((name) => { if (appRouter.requires.every((existing) => existing.name !== name)) { appRouter.requires.push({ name }); } }); } static adjustMtaYamlManagedApprouter(yamlContent, projectName, businessSolution, businessService) { const appRouterName = `${projectName}-destination-content`; let appRouter = yamlContent.modules.find((module) => module.name === appRouterName); if (appRouter == null) { businessSolution = businessSolution.split(".").join("_"); appRouter = { name: appRouterName, type: this.SAP_APPLICATION_CONTENT, requires: [ { name: `${projectName}_uaa`, parameters: { "service-key": { name: `${projectName}-uaa-key` } } }, { name: `${projectName}_html_repo_host`, parameters: { "service-key": { name: `${projectName}-html_repo_host-key` } } }, { name: `${projectName}-destination`, parameters: { "content-target": true } }, { name: `${businessService}`, parameters: { "service-key": { name: `${businessService}-key` } } } ], "build-parameters": { "no-source": true }, parameters: { content: { instance: { destinations: [ { Name: `${businessSolution}-${projectName}-html_repo_host`, ServiceInstanceName: `${projectName}-html5_app_host`, ServiceKeyName: `${projectName}-html_repo_host-key`, "sap.cloud.service": businessSolution.replace(/_/g, ".") }, { Name: `${businessSolution}-uaa-${projectName}`, ServiceInstanceName: `${projectName}-xsuaa`, ServiceKeyName: `${projectName}_uaa-key`, Authentication: "OAuth2UserTokenExchange", "sap.cloud.service": businessSolution.replace(/_/g, ".") }, { Name: `${businessService}-service_instance_name`, Authentication: "OAuth2UserTokenExchange", ServiceInstanceName: `${businessService}`, ServiceKeyName: `${businessService}-key` } ], existing_destinations_policy: "update" } } } }; yamlContent.modules.push(appRouter); } } static adjustMtaYamlUDeployer(yamlContent, projectName, moduleName) { const uiDeployerName = `${projectName}_ui_deployer`; let uiDeployer = yamlContent.modules.find((module) => module.name === uiDeployerName); if (uiDeployer == null) { uiDeployer = { name: uiDeployerName, type: this.SAP_APPLICATION_CONTENT, path: ".", requires: [], "build-parameters": { "build-result": "resources", requires: [] } }; yamlContent.modules.push(uiDeployer); } const htmlRepoHostName = `${projectName}_html_repo_host`; if (uiDeployer.requires.every((req) => req.name !== htmlRepoHostName)) { uiDeployer.requires.push({ name: htmlRepoHostName, parameters: { "content-target": true } }); } if (uiDeployer["build-parameters"].requires.every((require) => require.name !== moduleName)) { uiDeployer["build-parameters"].requires.push({ artifacts: [`${moduleName}.zip`], name: moduleName, "target-path": "resources/" }); } } static adjustMtaYamlResources(yamlContent, projectName, isManagedAppRouter) { const resources = [ { name: `${projectName}_html_repo_host`, type: this.CF_MANAGED_SERVICE, parameters: { service: this.HTML5_APPS_REPO, "service-plan": "app-host", "service-name": `${projectName}-html5_app_host` } }, { name: `${projectName}_uaa`, type: this.CF_MANAGED_SERVICE, parameters: { service: "xsuaa", path: "./xs-security.json", "service-plan": "application", "service-name": `${YamlUtils.getProjectNameForXsSecurity()}-xsuaa` } } ]; if (isManagedAppRouter) { resources.push({ name: `${projectName}-destination`, type: this.CF_MANAGED_SERVICE, parameters: { service: "destination", "service-name": `${projectName}-destination`, "service-plan": "lite", config: { HTML5Runtime_enabled: true, version: "1.0.0" } } }); } else { resources.push({ name: `portal_resources_${projectName}`, type: this.CF_MANAGED_SERVICE, parameters: { service: "portal", "service-plan": "standard" } }, { name: `${projectName}_html_repo_runtime`, type: this.CF_MANAGED_SERVICE, parameters: { service: this.HTML5_APPS_REPO, "service-plan": "app-runtime" } }); } resources.forEach((resource) => { if (yamlContent.resources.every((existing) => existing.name !== resource.name)) { yamlContent.resources.push(resource); } }); } static adjustMtaYamlOwnModule(yamlContent, moduleName) { let module = yamlContent.modules.find((module) => module.name === moduleName); if (module == null) { module = { name: moduleName, type: "html5", path: moduleName, "build-parameters": { builder: "custom", commands: ["npm install", "npm run build"], "supported-platforms": [] } }; yamlContent.modules.push(module); } } static addModuleIfNotExists(requires, name) { if (requires.every((require) => require.name !== name)) { requires.push({ name }); } } static adjustMtaYamlFlpModule(yamlContent, projectName, businessService) { yamlContent.modules.forEach((module, index) => { if (module.type === this.SAP_APPLICATION_CONTENT && module.requires) { const portalResources = module.requires.find((require) => require.name === `portal_resources_${projectName}`); if (lodash_1.default.get(portalResources, ["parameters", "service-key", "name"]) === "content-deploy-key") { this.addModuleIfNotExists(module.requires, `${projectName}_html_repo_host`); this.addModuleIfNotExists(module.requires, `${projectName}_ui_deployer`); this.addModuleIfNotExists(module.requires, businessService); // move flp module to last position yamlContent.modules.push(yamlContent.modules.splice(index, 1)[0]); } } }); } } exports.default = YamlUtils; YamlUtils.STANDALONE_APPROUTER = "Standalone Approuter"; YamlUtils.APPROUTER_MANAGED = "Approuter Managed by SAP Cloud Platform"; YamlUtils.MTA_YAML_FILE = "mta.yaml"; YamlUtils.UI5_YAML_FILE = "ui5.yaml"; YamlUtils.HTML5_APPS_REPO = "html5-apps-repo"; YamlUtils.SAP_APPLICATION_CONTENT = "com.sap.application.content"; YamlUtils.CF_MANAGED_SERVICE = "org.cloudfoundry.managed-service"; //# sourceMappingURL=YamlUtils.js.map