UNPKG

@sap/generator-adaptation-project

Version:

Adaptation project allows you to create an app variant for an existing SAP Fiori elements-based or SAPUI5 freestyle application, without changing the original application.

279 lines 15.1 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const AdaptationProjectGeneratorBase_1 = __importDefault(require("./AdaptationProjectGeneratorBase")); const yeoman_ui_types_1 = require("@sap-devx/yeoman-ui-types"); const adp_common_1 = require("@sap/adp-common"); const adp_cf_1 = require("@sap/adp-cf"); const adp_abap_1 = require("@sap/adp-abap"); const validator_1 = require("../questions/validator"); class SubGeneratorWithAuthBase extends AdaptationProjectGeneratorBase_1.default { constructor(args, opts, type) { super(args, opts, type); this.generatorType = type; try { if (opts.data) { this.projectPath = opts.data.path; this.projectData = adp_common_1.Workspace.project(this.projectPath); this.logger.log(adp_common_1.Messages.SUCCESSFULLY_RETRIEVED_PROJECT_DATA(JSON.stringify(this.projectData, null, 2))); this.isCFEnv = this.projectData.environment === "CF"; this.fdcService = new adp_cf_1.FDCService(); } this.vscode = opts.vscode; } catch (e) { this.validationError = e; } this._setPrompts(); } onInit() { return __awaiter(this, void 0, void 0, function* () { if (this.validationError) { this.appWizard.showError(this.validationError.message, yeoman_ui_types_1.MessageType.notification); yield this.handleRuntimeCrash(this.validationError.message, false); } else { try { this.isInternalUsage = adp_common_1.EnvironmentUtils.isInternalUsage(); this.requiresAuthentication = yield adp_common_1.Workspace.getProjectRequiresAuthentication(this.projectData, this.fdcService); this.logger.log(adp_common_1.Messages.DESTINATION_REQUIRES_AUTHENTICATION(this.requiresAuthentication, this.projectData.sourceSystem)); if (!this.requiresAuthentication) { this.prompts.splice(0, 1, []); } } catch (error) { yield this.handleRuntimeCrash(error.message, this.isCFEnv); } } }); } getManifest() { var _a; return __awaiter(this, void 0, void 0, function* () { if (this.requiresAuthentication) { const credentials = (yield this.prompt(yield this.getCredentialsPrompts(this.projectData, this.fdcService, this.vscode))); adp_abap_1.ManifestServiceABAP.setCredentials(credentials); } const manifest = yield adp_common_1.ManifestService.getManifest(this.projectData, adp_cf_1.ManifestServiceCF, adp_abap_1.ManifestServiceABAP); const oDataSources = ((_a = manifest["sap.app"]) === null || _a === void 0 ? void 0 : _a.dataSources) || []; this.oDataSources = oDataSources; this.logger.log(adp_common_1.Messages.ODATA_SOURCES_FROM_MANIFEST(JSON.stringify(oDataSources, null, 2))); this.oDataTargetSources = adp_common_1.ManifestService.getTargetODataSources(oDataSources); this.logger.log(adp_common_1.Messages.ODATA_TARGET_SOURCES(JSON.stringify(this.oDataTargetSources, null, 2))); this.manifest = manifest; }); } _setPrompts() { this.setPromptsCallback = (fn) => { // setPromptsCallback gets called only in Yeoman UI. Thus we can use it to know the running context. if (this.prompts) { this.prompts.setCallback(fn); } }; if (this.validationError) { this.prompts = new yeoman_ui_types_1.Prompts(this._errorPage(this.generatorType)); } else { this.prompts = new yeoman_ui_types_1.Prompts(this.getSubGeneratorsWithAuthenticationPages(this.generatorType, this.isCFEnv, this.projectData.sourceSystem)); } } _errorPage(subGenType) { switch (subGenType) { case adp_common_1.GeneratorTypes.ADD_ANNOTATIONS_TO_DATA: { return [{ name: "Add Local Annotation File", description: "" }]; } case adp_common_1.GeneratorTypes.CHANGE_DATA_SOURCE: { return [{ name: "Replace OData Service", description: "" }]; } default: { return []; } } } getSubGeneratorsWithAuthenticationPages(subGenType, isCFEnv, destination) { switch (subGenType) { case adp_common_1.GeneratorTypes.ADD_ANNOTATIONS_TO_DATA: { const credentialsPageName = isCFEnv ? adp_common_1.Messages.LOGIN_CF : "Add Local Annotation File - Credentials"; const credentialsPageDescription = isCFEnv ? adp_common_1.Messages.PROVIDE_CREDENTIALS : `Enter credentials for your adaptation project's system (${destination})`; return [ { name: credentialsPageName, description: credentialsPageDescription }, { name: "Add Local Annotation File", description: "Select OData Service and Annotation XML" } ]; } case adp_common_1.GeneratorTypes.CHANGE_DATA_SOURCE: { const credentialsPageName = isCFEnv ? adp_common_1.Messages.LOGIN_CF : "Replace OData Service - Credentials"; const credentialsPageDescription = isCFEnv ? adp_common_1.Messages.PROVIDE_CREDENTIALS : `Enter credentials for your adaptation project's system (${destination})`; return [ { name: credentialsPageName, description: credentialsPageDescription }, { name: "Replace OData Service", description: "Select OData Service and new OData URI" } ]; } default: return []; } } getCredentialsPrompts(projectData, fdcService, vscode) { return __awaiter(this, void 0, void 0, function* () { switch (projectData.environment) { case "CF": { return yield this.getCFExternalLoginPrompts(projectData, fdcService, vscode); } case "ABAP": { return this.getABAPCredentialsPrompts(projectData); } case "S4HANACLOUD": { return this.getABAPCredentialsPrompts(projectData); } default: throw new Error("Unknown environment:" + projectData.environment); } }); } getABAPCredentialsPrompts(projectData) { return [this.getUsernamePrompt(), this.getABAPPasswordPrompt(projectData)]; } getCFExternalLoginPrompts(projectData, fdcService, vscode) { return __awaiter(this, void 0, void 0, function* () { const isExternalLoginEnabled = yield fdcService.isExternalLoginEnabled(); const shouldNotExternalLogin = !isExternalLoginEnabled && !adp_common_1.EnvironmentUtils.isRunningInBAS(); let isLoggedToDifferentSource = yield fdcService.isLoggedInToDifferentSource(projectData.cfOrganization, projectData.cfSpace, projectData.cfApiUrl); let cfConfig = fdcService.getConfig(); const messagePrompts = isLoggedToDifferentSource ? this.getExternalLoginWrongSourceMessagesPrompts(projectData, cfConfig, !shouldNotExternalLogin) : this.getExternalLoginUnloggedMessagePrompts(projectData, !shouldNotExternalLogin); if (isLoggedToDifferentSource) { yield adp_cf_1.CFUtils.cFLogout(); } return [ ...messagePrompts, { type: "input", name: "cfExternalLogin", message: "", guiOptions: { type: "label" }, validate: () => __awaiter(this, void 0, void 0, function* () { if (shouldNotExternalLogin) { return adp_cf_1.Messages.EXTERNAL_LOGIN_DISABLED_MESSAGE; } while (cfConfig.org.name !== projectData.cfOrganization || cfConfig.space.name !== projectData.cfSpace || cfConfig.url !== projectData.cfApiUrl) { const externalLoginResult = yield vscode.commands.executeCommand("cf.login", "side"); if (externalLoginResult !== "OK" || !externalLoginResult) { yield adp_cf_1.CFUtils.cFLogout(); return adp_common_1.Messages.LOGIN_FAILED_MSG; } fdcService.loadConfig(); cfConfig = fdcService.getConfig(); isLoggedToDifferentSource = yield fdcService.isLoggedInToDifferentSource(projectData.cfOrganization, projectData.cfSpace, projectData.cfApiUrl); if (isLoggedToDifferentSource) { yield adp_cf_1.CFUtils.cFLogout(); } } this.isLoginSuccessfull = true; return true; }) }, { type: "input", name: "cfExternalLoginSuccessMessage", message: adp_common_1.Messages.LOGIN_SUCCESS_MSG, guiOptions: { type: "label" }, when: () => this.isLoginSuccessfull } ]; }); } getExternalLoginWrongSourceMessagesPrompts(projectData, cfConfig, shouldShowPrompt) { return [ this.getMessagePrompt("cfExternalLoginWrongSourceMainMessage", adp_cf_1.Messages.EXTERNAL_LOGIN_WRONG_SOURCE_MAIN_MESSAGE, shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceAPIEnpointMessage", adp_cf_1.Messages.CF_API_ENDPOINT(cfConfig.url), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceOrganizationMessage", adp_cf_1.Messages.CF_ORGANIZATION(cfConfig.org.name), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceSpaceMessage", adp_cf_1.Messages.CF_SPACE(cfConfig.space.name), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceMiddleMessage", adp_cf_1.Messages.EXTERNAL_LOGIN_WRONG_SOURCE_MIDDLE_MESSAGE, shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceProjectApiUrlMessage", adp_cf_1.Messages.CF_API_ENDPOINT(projectData.cfApiUrl), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceProjectOrganizationMessage", adp_cf_1.Messages.CF_ORGANIZATION(projectData.cfOrganization), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceProjectSpaceMessage", adp_cf_1.Messages.CF_SPACE(projectData.cfSpace), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginWrongSourceEndingMessage", adp_cf_1.Messages.EXTERNAL_LOGIN_WRONG_SOURCE_ENDING_MESSAGE, shouldShowPrompt) ]; } getExternalLoginUnloggedMessagePrompts(projectData, shouldShowPrompt) { return [ this.getMessagePrompt("cfExternalLoginMainMessage", adp_cf_1.Messages.EXTERNAL_LOGIN_MESSAGE_MANIFEST_APPDESCR, shouldShowPrompt), this.getMessagePrompt("cfExternalLoginProjectApiUrlMessage", adp_cf_1.Messages.CF_API_ENDPOINT(projectData.cfApiUrl), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginProjectOrganizationMessage", adp_cf_1.Messages.CF_ORGANIZATION(projectData.cfOrganization), shouldShowPrompt), this.getMessagePrompt("cfExternalLoginProjectSpaceMessage", adp_cf_1.Messages.CF_SPACE(projectData.cfSpace), shouldShowPrompt) ]; } getMessagePrompt(name, message, shouldShow) { return { type: "input", name: name, message: message, guiOptions: { type: "label" }, when: () => shouldShow }; } getUsernamePrompt() { return { type: "input", name: "username", message: adp_common_1.Messages.USERNAME, validate: (value) => { return (0, validator_1.validateForEmptyValue)(value, adp_common_1.Messages.USERNAME); }, guiOptions: { mandatory: true }, store: false }; } getABAPPasswordPrompt(projectData) { return { type: "password", guiType: "login", name: "password", message: adp_common_1.Messages.PASSWORD, mask: "*", validate: (value, answers) => __awaiter(this, void 0, void 0, function* () { if (value.length <= 0) { return adp_common_1.Messages.INPUT_CANNOT_BE_EMPTY_ERROR(adp_common_1.Messages.PASSWORD); } if (!answers.username) { return adp_common_1.Messages.ERROR_MISSING_REQUIRED_DATA; } try { const endpointsManager = yield adp_common_1.EndpointsManager.getInstance(); const abapProvider = new adp_abap_1.AbapProvider(endpointsManager); const abapService = new adp_abap_1.AbapService(abapProvider, adp_common_1.EnvironmentUtils.isInternalUsage()); yield abapProvider.setProvider(projectData.sourceSystem, projectData.client, answers.username, value); yield abapService.getSystemUI5Version(); return true; } catch (error) { return adp_common_1.MessageUtils.getLoginErrorMessage(error === null || error === void 0 ? void 0 : error.response); } }), guiOptions: { mandatory: true }, store: false }; } } module.exports = SubGeneratorWithAuthBase; //# sourceMappingURL=SubGeneratorWithAuthBase.js.map