@sap/adp-common
Version:
common logic for all yeoman generators
335 lines • 17.2 kB
JavaScript
"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 };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
const messages_1 = require("../i18n/messages");
const logger_1 = require("../logger");
const axios_1 = __importDefault(require("axios"));
const LATEST = "latest";
const SAP_UI5_VERSION_API = "https://sapui5.hana.ondemand.com/version.json";
class UI5VersionsUtils {
static setInternalState(isInternal) {
this.isInternal = isInternal;
}
static getSystemRelevantVersions(version) {
return __awaiter(this, void 0, void 0, function* () {
const versionPattern = /^[1-9]\.\d{1,3}\.\d{1,2}\.*/;
this.detectedVersion = !!version && versionPattern.test(version);
this.systemVersion = this.detectedVersion ? version : undefined;
return yield this.getRelevantVersions(this.systemVersion);
});
}
static getDetectedVersion() {
return this.detectedVersion;
}
static getSystemVersion() {
return this.systemVersion;
}
static shouldSetMinUI5Version() {
if (!this.getDetectedVersion()) {
return false;
}
const versionParts = this.getSystemVersion().split(".");
return parseInt(versionParts[1]) >= 90; // should match released version (e.g. 1.92.4) and snapshot or latest
}
static getRelevantVersions(version) {
return __awaiter(this, void 0, void 0, function* () {
// for internal users returns all internally available versions
// for external shows all higher versions than the one on the system
// if the version is not detected shows the latest released version
const allPublicVersions = yield this.getPublicVersions();
let relevantVersions;
let formattedVersion;
let systemSnapshotVersion;
let systemLatestVersion;
if (version) {
formattedVersion = this.removeTimestampFromVersion(version);
this.systemVersion = formattedVersion;
systemSnapshotVersion = this.addSnapshot(version);
systemLatestVersion = formattedVersion === allPublicVersions["latest"]["version"] ? this.LATEST_VERSION : "";
}
if (this.isInternal) {
relevantVersions = yield this.getInternalVersions();
if (version) {
let relevantVersionsAsString = relevantVersions.join();
const formattedVersionRegex = new RegExp(formattedVersion + " ", "g");
relevantVersionsAsString = relevantVersionsAsString.replace(formattedVersionRegex, `${formattedVersion}${systemSnapshotVersion} ${this.CURRENT_SYSTEM_VERSION}`);
relevantVersions = relevantVersionsAsString.split(",");
relevantVersions.unshift(`${formattedVersion}${systemSnapshotVersion} ${this.CURRENT_SYSTEM_VERSION + systemLatestVersion}`);
}
relevantVersions.unshift(this.SNAPSHOT_VERSION);
relevantVersions.unshift(this.SNAPSHOT_UNTESTED_VERSION);
}
else {
if (version && systemSnapshotVersion === "") {
relevantVersions = yield this.getHigherVersions(formattedVersion);
if (!relevantVersions.length && formattedVersion !== allPublicVersions["latest"]["version"]) {
relevantVersions = [`${formattedVersion} ${this.CURRENT_SYSTEM_VERSION}`, `${allPublicVersions["latest"]["version"]} ${this.LATEST_VERSION}`];
}
else {
relevantVersions.unshift(`${formattedVersion} ${this.CURRENT_SYSTEM_VERSION + systemLatestVersion}`);
}
}
else {
relevantVersions = [`${allPublicVersions["latest"]["version"]} ${this.LATEST_VERSION}`];
}
}
return [...new Set(relevantVersions)];
});
}
static getPublicVersions() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.publicVersions) {
const response = yield axios_1.default.get("https://sapui5.hana.ondemand.com/version.json");
this.publicVersions = response.data;
this.latestVersion = this.publicVersions["latest"]["version"];
}
return this.publicVersions;
});
}
static getInternalVersions() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.releasedVersions) {
const response = yield axios_1.default.get("https://ui5.sap.com/neo-app.json");
this.releasedVersions = response.data.routes.map((route) => {
const version = route.target.version === this.latestVersion ? `${route.target.version} ${this.LATEST_VERSION}` : route.target.version;
return version;
});
}
return this.releasedVersions.filter(this.isFeatureSupportedVersion.bind(this, "1.71.0"));
});
}
static isFeatureSupportedVersion(featureVersion, version) {
if (!version || !featureVersion) {
return false;
}
const snapshotVersions = ["snapshot", "snapshot-untested"];
// Checks if version is higher or equal to the version from which the feature is introduced
const featureVersionParts = featureVersion.split(".");
const versionParts = version.split(".");
const snapshotVersion = version.split("-");
// When feature version 2.* (or n.*) is bigger than version that is passed we return false
if (parseInt(featureVersionParts[0]) > parseInt(version[0])) {
return false;
}
return ((snapshotVersions.includes(snapshotVersion[0]) &&
(parseInt(versionParts[0].slice(-1)) > parseInt(featureVersionParts[0]) || parseInt(versionParts[1]) >= parseInt(featureVersionParts[1]))) ||
snapshotVersions.includes(version) ||
version.length === 0 ||
parseInt(versionParts[0]) > parseInt(featureVersionParts[0]) ||
(parseInt(versionParts[0]) === parseInt(featureVersionParts[0]) && parseInt(versionParts[1]) > parseInt(featureVersionParts[1])) ||
(parseInt(versionParts[0]) === parseInt(featureVersionParts[0]) &&
parseInt(versionParts[1]) === parseInt(featureVersionParts[1]) &&
parseInt(versionParts[2]) >= parseInt(featureVersionParts[2])));
}
static getLatestPublicVersion() {
return this.latestVersion;
}
static getHigherVersions(version) {
return __awaiter(this, void 0, void 0, function* () {
const allPublicVersions = yield this.getPublicVersions();
const versionParts = version.split(".");
const minorVersion = parseInt(versionParts[1]);
const microVersion = parseInt(versionParts[2]);
let versions = "";
Object.keys(allPublicVersions).forEach((publicVersionKey) => {
const versionArr = allPublicVersions[publicVersionKey]["version"].split(".");
if (parseInt(versionArr[1]) > minorVersion || (parseInt(versionArr[1]) == minorVersion && parseInt(versionArr[2]) > microVersion)) {
versions += allPublicVersions[publicVersionKey]["version"] + ",";
}
});
const latestVersionRegex = new RegExp(allPublicVersions["latest"]["version"], "g");
const versionsLatest = versions.replace(latestVersionRegex, `${allPublicVersions["latest"]["version"]} ${this.LATEST_VERSION}`);
const result = versionsLatest.split(",");
result.pop();
return result.reverse();
});
}
static getOfficialBaseUI5VersionUrl(version) {
if (version.toLowerCase().includes("snapshot")) {
return "https://sapui5preview-sapui5.dispatcher.int.sap.eu2.hana.ondemand.com:443";
}
return "https://ui5.sap.com";
}
static getDestinationUI5Name(version) {
if (this.isInternal && version.toLowerCase().includes("snapshot")) {
return `sapui5preview-sapui5`;
}
return `sapui5`;
}
static getDestinationUI5Path(version) {
if (!this.isInternal) {
return ``;
}
const versionIs = version.toLowerCase().includes("-snapshot") ? "snapshot-onsystem" : version;
switch (versionIs) {
case this.SNAPSHOT_VERSION:
return `/snapshot/`;
case this.SNAPSHOT_UNTESTED_VERSION:
return `/snapshot-untested/`;
case "snapshot-onsystem": {
const snapshotVersion = this.removeMicroPart(this.removeBracketsFromVersion(version));
return `/snapshot-${snapshotVersion}/`;
}
default:
return ``;
}
}
static removeBracketsFromVersion(version) {
// removes additional information about the selected version (e.g. "latest")
if (version.indexOf("(") !== -1) {
const versionParts = version.split("(");
return versionParts[0].trim();
}
return version;
}
static removeTimestampFromVersion(version) {
// removes timestamp part in case the version taken from the system is snapshot
// converts 1.95.0.34566363464 --> 1.95.0
const versionParts = version.split(".");
return `${versionParts[0]}.${versionParts[1]}.${versionParts[2]}`;
}
static addSnapshot(version) {
// adds "snapshot" suffix for snapshot versions taken from selected system
// only if the snapshot is not already released
const versionParts = version.split(".");
return versionParts[3] && this.removeTimestampFromVersion(version) != this.getLatestPublicVersion() ? "-snapshot" : "";
}
static removeMicroPart(version) {
// snapshot url contains only the number of the version without micro part (e.g. transforms 1.87.3 --> 1.87)
const versionParts = version.split(".");
return `${versionParts[0]}.${versionParts[1]}`;
}
static validate(version) {
var _b, _c, _d, _e, _f;
return __awaiter(this, void 0, void 0, function* () {
if (version) {
const selectedVersionURL = UI5VersionsUtils.getOfficialBaseUI5VersionUrl(version);
const resource = version.includes("snapshot") ? "neo-app.json" : UI5VersionsUtils.getFormattedVersion(version);
try {
yield axios_1.default.get(`${selectedVersionURL}/${resource}`, { timeout: 3000 });
return true;
}
catch (e) {
if (version.includes("snapshot")) {
(_b = logger_1.Logger.getLogger) === null || _b === void 0 ? void 0 : _b.log(`[ADP Creation] Error on validating ui5 snapshot version: ${e}`);
return `${messages_1.Messages.UI5_VERSION_NOT_REACHABLE.replace("<URL>", selectedVersionURL)}`;
}
if (((_c = e.response) === null || _c === void 0 ? void 0 : _c.status) === 400 || ((_d = e.response) === null || _d === void 0 ? void 0 : _d.status) === 404) {
(_e = logger_1.Logger.getLogger) === null || _e === void 0 ? void 0 : _e.log(`[ADP Creation] Error on validating ui5 version: ${e}`);
return `${messages_1.Messages.UI5_VERSION_NOT_AVAILABLE}`;
}
(_f = logger_1.Logger.getLogger) === null || _f === void 0 ? void 0 : _f.log(`[ADP Creation] Error on validating ui5 version: ${e}`);
return `Error on validating ui5 version: ${e}`;
}
}
return messages_1.Messages.VERSION_CANNOT_BE_EMPTY;
});
}
static getFormattedVersion(version) {
// remove additional information from version number
// reverse "specified" snapshot version 1.96.0-snapshot --> snapshot-1.96
version = this.removeBracketsFromVersion(version);
return version.toLowerCase().includes("-snapshot") ? `snapshot-${this.removeMicroPart(version)}` : version;
}
static getVersionToBeUsed(version) {
// in case we are in external mode but system has snapshot version
// we should use the latest public version instead
// in case S4HANACLOUD end is selected we are using the latest public version
if (!version || (!this.isInternal && version.includes("snapshot"))) {
return this.getLatestPublicVersion();
}
return version;
}
static postUI5VersionRequest(serviceUrl) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield axios_1.default.get(serviceUrl);
return response.data;
});
}
static getUI5Version() {
return __awaiter(this, void 0, void 0, function* () {
try {
const ui5VersionsRequest = yield this.postUI5VersionRequest(SAP_UI5_VERSION_API);
if (ui5VersionsRequest && ui5VersionsRequest[LATEST] && ui5VersionsRequest[LATEST].version) {
return ui5VersionsRequest[LATEST].version;
}
throw new Error(messages_1.Messages.ERROR_PARSING_UI5_VERSIONS);
}
catch (e) {
throw new Error(messages_1.Messages.ERROR_RETRIEVING_UI5_VERSIONS);
}
});
}
static isExistingUI5Version(version) {
return __awaiter(this, void 0, void 0, function* () {
const cyrillicPattern = /^\p{Script=Cyrillic}+$/u;
if (cyrillicPattern.test(version)) {
return false;
}
const selectedVersionURL = version.includes("snapshot")
? `http://sapui5preview-sapui5.dispatcher.int.sap.eu2.hana.ondemand.com:443/${version}/neo-app.json`
: `https://sapui5.hana.ondemand.com/${version}/`;
try {
yield axios_1.default.get(selectedVersionURL);
// The request was made and the server responded with a status code
// that falls in the range of 2xx
return true;
}
catch (error) {
if (error.response.status === 400 || error.response.status === 404) {
return false;
}
else {
throw new Error(`${error.response.status}: ${error.response.statusText}`);
}
}
});
}
static isSupportedVersion(version) {
const versionParts = version.split(".");
const snapshotVersion = version.split("-");
return ((this.SNAPSHOT_VERSIONS.includes(snapshotVersion[0]) && (parseInt(versionParts[0].slice(-1)) > 1 || parseInt(versionParts[1]) >= 71)) ||
this.SNAPSHOT_VERSIONS.includes(version) ||
version.length === 0 ||
parseInt(versionParts[0]) > 1 ||
parseInt(versionParts[1]) >= 71);
}
static getDestinationUI5Version(destinationName, defaultVersion) {
return __awaiter(this, void 0, void 0, function* () {
try {
const response = yield axios_1.default.get(`${process.env.H2O_URL}/destinations/${destinationName}/sap/bc/adt/filestore/ui5-bsp/ui5-rt-version`);
return this.getStrippedSystemUI5Version(response.data);
}
catch (_b) {
return defaultVersion;
}
});
}
static getStrippedSystemUI5Version(ui5Version) {
const ui5VersionArr = ui5Version.split(".");
if (ui5VersionArr[3]) {
ui5VersionArr.pop();
}
return ui5VersionArr.join(".");
}
}
exports.default = UI5VersionsUtils;
_a = UI5VersionsUtils;
UI5VersionsUtils.CURRENT_SYSTEM_VERSION = "(system version)";
UI5VersionsUtils.LATEST_VERSION = "(latest)";
UI5VersionsUtils.SNAPSHOT_VERSION = "snapshot";
UI5VersionsUtils.SNAPSHOT_UNTESTED_VERSION = "snapshot-untested";
UI5VersionsUtils.SNAPSHOT_VERSIONS = [_a.SNAPSHOT_VERSION, _a.SNAPSHOT_UNTESTED_VERSION];
//# sourceMappingURL=UI5VersionsUtils.js.map