@ui5/task-adaptation
Version:
Custom task for ui5-builder which allows building UI5 Flexibility Adaptation Projects for SAP BTP, Cloud Foundry environment
72 lines • 3.83 kB
JavaScript
import * as dotenv from "dotenv";
import { logBuilderVersion } from "./util/commonUtil.js";
import AppVariant from "./appVariantManager.js";
import BaseApp from "./baseAppManager.js";
import I18nMerger from "./util/i18nMerger.js";
import ResourceUtil from "./util/resourceUtil.js";
import { determineProcessor } from "./processors/processor.js";
import FilesUtil from "./util/filesUtil.js";
/**
* Creates an appVariant bundle from the provided resources.
*/
export default ({ workspace, options, taskUtil }) => {
dotenv.config();
async function process(workspace, taskUtil) {
logBuilderVersion();
const processor = determineProcessor(options.configuration);
const adaptationProject = await AppVariant.fromWorkspace(workspace, options.projectNamespace);
const appVariantIdHierarchy = await processor.getAppVariantIdHierarchy(adaptationProject.reference);
if (appVariantIdHierarchy.length === 0) {
throw new Error(`No app variant found for reference ${adaptationProject.reference}`);
}
// appVariantIdHierarchy contains original application on bottom and the
// latest app variant on top. We reverse the list to process original
// application first and then app variants in chronological order.
const reversedHierarchy = appVariantIdHierarchy.toReversed();
const fetchFilesPromises = reversedHierarchy.map(({ repoName, cachebusterToken }) => processor.fetch(repoName, cachebusterToken));
fetchFilesPromises.push(Promise.resolve(adaptationProject.files));
const appVariants = new Array();
const adapt = async (baseAppFiles, appVariantFiles) => {
let baseApp = BaseApp.fromFiles(baseAppFiles);
let appVariant = AppVariant.fromFiles(appVariantFiles);
// If the app variant is the same as the adaptation project, we use the
// adaptation project because it contains resources that should be updated.
if (appVariant.id === adaptationProject.id) {
appVariant = adaptationProject;
}
appVariants.push(appVariant);
const adaptedFiles = await baseApp.adapt(appVariant, processor);
return I18nMerger.merge(adaptedFiles, baseApp.i18nPath, appVariant);
};
let files = await fetchFilesPromises.reduce(async (previousFiles, currentFiles) => adapt(await previousFiles, await currentFiles), fetchFilesPromises.shift());
const references = getReferences(appVariants, adaptationProject.id);
files = FilesUtil.filter(files);
files = FilesUtil.rename(files, references);
adaptationProject.omitDeletedResources(files, options.projectNamespace, taskUtil);
const writePromises = new Array();
files.forEach((content, filename) => {
const resource = ResourceUtil.createResource(filename, options.projectNamespace, content);
writePromises.push(workspace.write(resource));
});
await Promise.all(writePromises);
}
return process(workspace, taskUtil);
/**
* 4p. Reference map contains searchTerm as key and replacement as value. Base
* id and app variant ids are renamed to adaptation project id.
*/
function getReferences(appVariants, adaptationProjectId) {
const references = new Map();
appVariants.forEach((variant, i) => {
if (i === 0) {
references.set(variant.reference, adaptationProjectId);
}
if (variant.id !== adaptationProjectId) {
references.set(variant.id, adaptationProjectId);
}
variant.getRenamingForMovedFiles().forEach((value, key) => references.set(key, value));
});
return references;
}
};
//# sourceMappingURL=index.js.map