Skip to content

Commit

Permalink
[INTERNAL] Adjust JSDoc and optimize stringify
Browse files Browse the repository at this point in the history
- adjust JSDoc for flexChangesBundler
- reduce number of needed JSON.stringify
  • Loading branch information
alexeng06 committed Jan 16, 2024
1 parent dc92641 commit a2d6ed7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
29 changes: 13 additions & 16 deletions lib/processors/bundlers/flexChangesBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -88,39 +88,39 @@ export default function({resources, options: {pathPrefix, hasFlexBundleVersion},
return JSON.stringify(changes);
} else {
bundleName = "flexibility-bundle.json";
const newChangeFormat = {
let newChangeFormat = {
changes,
compVariants,
variants,
variantChanges,
variantDependentControlChanges,
variantManagementChanges
};

if (Object.keys(existingFlexBundle).length > 0) {
newChangeFormat = mergeFlexChangeBundles(newChangeFormat, existingFlexBundle);
}
return JSON.stringify(newChangeFormat);
}
}

/**
* 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) => {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/bundlers/generateFlexChangesBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit a2d6ed7

Please sign in to comment.