@sap/adp-abap
Version:
abap service for all yeoman generators
147 lines • 6.48 kB
JavaScript
;
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.ApplicationManager = exports.mapApps = exports.filterApps = exports.ABAP_VARIANT_APPS_PARAMS = exports.ABAP_APPS_PARAMS = exports.S4HANA_APPS_PARAMS = void 0;
const adp_common_1 = require("@sap/adp-common");
exports.S4HANA_APPS_PARAMS = {
"sap.app/type": "application",
"sap.fiori/cloudDevAdaptationStatus": "released",
"fileType": "appdescr",
"fields": "sap.app/id,repoName,sap.fiori/cloudDevAdaptationStatus,sap.app/ach,sap.fiori/registrationIds,sap.app/title,url,fileType"
};
exports.ABAP_APPS_PARAMS = {
"fields": "sap.app/id,sap.app/ach,sap.fiori/registrationIds,sap.app/title,url,fileType,repoName",
"sap.ui/technology": "UI5",
"sap.app/type": "application",
"fileType": "appdescr"
};
exports.ABAP_VARIANT_APPS_PARAMS = {
"fields": "sap.app/id,sap.app/ach,sap.fiori/registrationIds,sap.app/title,url,fileType,repoName",
"sap.ui/technology": "UI5",
"sap.app/type": "application",
"fileType": "appdescr_variant",
"originLayer": "VENDOR"
};
/**
* Function that gets property value from Record<string, unknown>.
* @param {Record<string, unknown>} record - The record object.
* @param {string} key - The key which value should be returned.
* @returns {T | undefined} - returns property value if exists otherwise undefined.
*/
function getPropertyValue(record, key) {
const value = record[key];
return value ? value : undefined;
}
/**
* Compares two applications for sorting, using the title and falling back to the ID if titles are missing or equal.
* This function ensures that applications are sorted alphabetically by their title or ID in a case-insensitive manner.
*
* @param {Application} appA - The first application to compare.
* @param {Application} appB - The second application to compare.
* @returns {number} A number indicating the sort order.
*/
const filterApps = (appA, appB) => {
let titleA = appA.title.toUpperCase();
let titleB = appB.title.toUpperCase();
if (!titleA.trim()) {
titleA = appA.id.toUpperCase();
}
if (!titleB.trim()) {
titleB = appB.id.toUpperCase();
}
if (titleA < titleB) {
return -1;
}
if (titleA > titleB) {
return 1;
}
return 0;
};
exports.filterApps = filterApps;
/**
* Transforms raw application data into a structured Application object.
* This function maps properties from a loosely typed app data structure to a strongly typed Application object.
*
* @param {Partial<App>} app - The raw application data, possibly incomplete.
* @returns {Application} A structured application object with defined properties, even if some may be empty.
*/
const mapApps = (app) => {
var _a, _b, _c, _d, _e, _f, _g;
return ({
id: (_a = app["sap.app/id"]) !== null && _a !== void 0 ? _a : "",
title: (_b = app["sap.app/title"]) !== null && _b !== void 0 ? _b : "",
ach: (_c = getPropertyValue(app, "sap.app/ach")) !== null && _c !== void 0 ? _c : "",
registrationIds: (_d = app["sap.fiori/registrationIds"]) !== null && _d !== void 0 ? _d : [],
fileType: (_e = getPropertyValue(app, "fileType")) !== null && _e !== void 0 ? _e : "",
bspUrl: (_f = app["url"]) !== null && _f !== void 0 ? _f : "",
bspName: (_g = getPropertyValue(app, "repoName")) !== null && _g !== void 0 ? _g : ""
});
};
exports.mapApps = mapApps;
/**
* Provides services related to managing and loading applications from an ABAP provider.
*/
class ApplicationManager {
/**
* Constructs an instance of ApplicationManager.
*
* @param {AbapProvider} provider - The ABAP provider service.
* @param {boolean} isCustomerBase - Indicates if the current base is a customer base, which affects how applications are loaded.
*/
constructor(provider, isCustomerBase) {
this.provider = provider;
this.isCustomerBase = isCustomerBase;
this.applications = [];
}
/**
* Clears the stored list of applications.
*/
resetApps() {
this.applications = [];
}
/**
* Retrieves the currently loaded list of applications.
*
* @returns {Application[]} An array of applications.
*/
getApps() {
return this.applications;
}
/**
* Loads applications based on system type and user parameters, merging results from different app sources as needed.
*
* @param {boolean} isCloudSystem - Determines if the system is a cloud system, affecting which parameters to use for app searching.
* @returns {Application[]} list of applications.
* @throws {Error} Throws an error if the app data cannot be loaded.
*/
loadApps(isCloudSystem) {
return __awaiter(this, void 0, void 0, function* () {
let result = [];
const provider = this.provider.getProvider();
const appIndex = provider.getAppIndex();
try {
result = yield appIndex.search(isCloudSystem ? exports.S4HANA_APPS_PARAMS : exports.ABAP_APPS_PARAMS);
if (!isCloudSystem && this.isCustomerBase) {
const extraApps = yield appIndex.search(exports.ABAP_VARIANT_APPS_PARAMS);
result = result.concat(extraApps);
}
this.applications = result.map(exports.mapApps).sort(exports.filterApps);
return this.applications;
}
catch (e) {
adp_common_1.Logger.getLogger.error(`Could not load apps: ${e.message}`);
throw new Error("Cannot load applications from the system.");
}
});
}
}
exports.ApplicationManager = ApplicationManager;
//# sourceMappingURL=ApplicationManager.js.map