From a2d6ed76d831c955651a3ea78a66591af176aacc Mon Sep 17 00:00:00 2001 From: Alex Engel Date: Tue, 16 Jan 2024 12:45:39 +0100 Subject: [PATCH] [INTERNAL] Adjust JSDoc and optimize stringify - adjust JSDoc for flexChangesBundler - reduce number of needed JSON.stringify --- lib/processors/bundlers/flexChangesBundler.js | 29 +++++++++---------- .../bundlers/generateFlexChangesBundle.js | 6 ++-- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/processors/bundlers/flexChangesBundler.js b/lib/processors/bundlers/flexChangesBundler.js index 3cf9658e4..e0bd95980 100644 --- a/lib/processors/bundlers/flexChangesBundler.js +++ b/lib/processors/bundlers/flexChangesBundler.js @@ -20,7 +20,7 @@ import {createResource} from "@ui5/fs/resourceFactory"; * @param {string} parameters.options.pathPrefix Prefix for bundle path * @param {string} parameters.options.hasFlexBundleVersion true if minUI5Version >= 1.73 than * create flexibility-bundle.json - * @param {object} parameters.existingFlexBundle Object with existing flexibility-bundle.json + * @param {object} [parameters.existingFlexBundle={}] Object with existing flexibility-bundle.json * to merge with new changes * @returns {Promise<@ui5/fs/Resource[]>} Promise resolving with flex changes bundle resources */ @@ -88,7 +88,7 @@ export default function({resources, options: {pathPrefix, hasFlexBundleVersion}, return JSON.stringify(changes); } else { bundleName = "flexibility-bundle.json"; - const newChangeFormat = { + let newChangeFormat = { changes, compVariants, variants, @@ -96,7 +96,9 @@ export default function({resources, options: {pathPrefix, hasFlexBundleVersion}, variantDependentControlChanges, variantManagementChanges }; - + if (Object.keys(existingFlexBundle).length > 0) { + newChangeFormat = mergeFlexChangeBundles(newChangeFormat, existingFlexBundle); + } return JSON.stringify(newChangeFormat); } } @@ -104,23 +106,21 @@ export default function({resources, options: {pathPrefix, hasFlexBundleVersion}, /** * merge new and existing bundles * - * @param {string} changesContent Array of resources files - * @param {object} existingChanges Object of existing flexBundle - * @returns {string} Json sting of changes and control variants + * @param {object} newFlexBundle Object with new content of flexibility-bundle.json + * @returns {object} Object with merged content of new and existing flexibility-bundle.json */ - function mergeFlexChangeBundles(changesContent, existingChanges) { - const newChangeContent = JSON.parse(changesContent); + function mergeFlexChangeBundles(newFlexBundle) { const result = {}; - Object.keys(newChangeContent).forEach((key) => { - if (existingChanges[key] && Array.isArray(existingChanges[key])) { - result[key] = existingChanges[key].concat(newChangeContent[key]); + Object.keys(newFlexBundle).forEach((key) => { + if (existingFlexBundle[key] && Array.isArray(existingFlexBundle[key])) { + result[key] = existingFlexBundle[key].concat(newFlexBundle[key]); } else { - result[key] = newChangeContent[key]; + result[key] = newFlexBundle[key]; } }); - return JSON.stringify(result); + return result; } return Promise.all(resources.map((resource) => { @@ -133,9 +133,6 @@ export default function({resources, options: {pathPrefix, hasFlexBundleVersion}, const result = []; if (nNumberOfChanges > 0) { changesContent = sortAndStringifyInFlexFormat(changesContent); - if (hasFlexBundleVersion && Object.keys(existingFlexBundle).length > 0) { - changesContent = mergeFlexChangeBundles(changesContent, existingFlexBundle); - } result.push(createResource({ path: `${pathPrefix}/changes/${bundleName}`, string: changesContent diff --git a/lib/tasks/bundlers/generateFlexChangesBundle.js b/lib/tasks/bundlers/generateFlexChangesBundle.js index dfa47d0aa..f51b2a92b 100644 --- a/lib/tasks/bundlers/generateFlexChangesBundle.js +++ b/lib/tasks/bundlers/generateFlexChangesBundle.js @@ -86,9 +86,9 @@ export default async function({workspace, taskUtil, options = {}}) { let flexBundle = {}; if (semver.compare(version, "1.73.0") >= 0) { hasFlexBundleVersion = true; - const flexBundleRessource = await workspace.byPath(`${pathPrefix}/changes/flexibility-bundle.json`); - if (flexBundleRessource) { - flexBundle = JSON.parse(await flexBundleRessource.getString()); + const flexBundleResource = await workspace.byPath(`${pathPrefix}/changes/flexibility-bundle.json`); + if (flexBundleResource) { + flexBundle = JSON.parse(await flexBundleResource.getString()); } } const processedResources = await flexChangesBundler({