builder/lib/processors/bundlers/flexChangesBundler.js

  1. import {getLogger} from "@ui5/logger";
  2. const log = getLogger("builder:processors:bundlers:flexChangesBundler");
  3. import {createResource} from "@ui5/fs/resourceFactory";
  4. /**
  5. * @public
  6. * @module @ui5/builder/processors/bundlers/flexChangesBundler
  7. */
  8. /**
  9. * Bundles all supplied changes.
  10. *
  11. * @public
  12. * @function default
  13. * @static
  14. *
  15. * @param {object} parameters Parameters
  16. * @param {@ui5/fs/Resource[]} parameters.resources List of resources to be processed
  17. * @param {object} parameters.options Options
  18. * @param {string} parameters.options.pathPrefix Prefix for bundle path
  19. * @param {string} parameters.options.hasFlexBundleVersion true if minUI5Version >= 1.73 than
  20. * create flexibility-bundle.json
  21. * @param {object} [parameters.existingFlexBundle={}] Object with existing flexibility-bundle.json
  22. * to merge with new changes
  23. * @returns {Promise<@ui5/fs/Resource[]>} Promise resolving with flex changes bundle resources
  24. */
  25. export default function({resources, options: {pathPrefix, hasFlexBundleVersion}, existingFlexBundle = {}}) {
  26. let bundleName = "changes-bundle.json";
  27. function sortByTimeStamp(a, b) {
  28. return a.creation > b.creation ? 1 : -1;
  29. }
  30. /**
  31. * bundle changes resource to json string
  32. *
  33. * @param {Array} changesContent Array of resources files
  34. * @returns {string} Json sting of changes and control variants
  35. */
  36. function sortAndStringifyInFlexFormat(changesContent) {
  37. changesContent = changesContent.sort(sortByTimeStamp);
  38. const changes = [];
  39. const variantDependentControlChanges = [];
  40. const compVariants = [];
  41. const variants = [];
  42. const variantChanges = [];
  43. const variantManagementChanges = [];
  44. changesContent.forEach(function(content) {
  45. if (content.layer === "VENDOR") {
  46. content.support.user = "SAP";
  47. }
  48. switch (content.fileType) {
  49. case "change":
  50. if (content.appDescriptorChange && (content.appDescriptorChange === "true" ||
  51. content.appDescriptorChange == true)) {
  52. break;
  53. }
  54. if (content.variantReference && content.variantReference !== "") {
  55. variantDependentControlChanges.push(content);
  56. } else {
  57. changes.push(content);
  58. }
  59. break;
  60. case "variant":
  61. compVariants.push(content);
  62. break;
  63. case "ctrl_variant":
  64. variants.push(content);
  65. break;
  66. case "ctrl_variant_change":
  67. variantChanges.push(content);
  68. break;
  69. case "ctrl_variant_management_change":
  70. variantManagementChanges.push(content);
  71. break;
  72. }
  73. });
  74. if (!hasFlexBundleVersion && (compVariants.length != 0 || variants.length != 0 || variantChanges.length != 0 ||
  75. variantDependentControlChanges.length != 0 || variantManagementChanges.length != 0)) {
  76. throw new Error(
  77. "There are some control variant changes in the changes folder. This only works with a " +
  78. "minUI5Version 1.73.0. Please update the minUI5Version in the manifest.json to 1.73.0 or higher");
  79. }
  80. // create changes-bundle.json
  81. if (!hasFlexBundleVersion) {
  82. return JSON.stringify(changes);
  83. } else {
  84. bundleName = "flexibility-bundle.json";
  85. let newChangeFormat = {
  86. changes,
  87. compVariants,
  88. variants,
  89. variantChanges,
  90. variantDependentControlChanges,
  91. variantManagementChanges
  92. };
  93. if (Object.keys(existingFlexBundle).length > 0) {
  94. newChangeFormat = mergeFlexChangeBundles(newChangeFormat);
  95. }
  96. return JSON.stringify(newChangeFormat);
  97. }
  98. }
  99. /**
  100. * Merge new and existing bundles
  101. *
  102. * @param {object} newFlexBundle Object with new content of flexibility-bundle.json
  103. * @returns {object} Object with merged content of new and existing flexibility-bundle.json
  104. */
  105. function mergeFlexChangeBundles(newFlexBundle) {
  106. const result = {};
  107. Object.keys(newFlexBundle).forEach((key) => {
  108. if (existingFlexBundle[key] && Array.isArray(existingFlexBundle[key])) {
  109. result[key] = existingFlexBundle[key].concat(newFlexBundle[key]);
  110. } else {
  111. result[key] = newFlexBundle[key];
  112. }
  113. });
  114. return result;
  115. }
  116. return Promise.all(resources.map((resource) => {
  117. return resource.getBuffer().then((buffer) => {
  118. return JSON.parse(buffer.toString());
  119. });
  120. })).then((changesContent) => {
  121. const nNumberOfChanges = changesContent.length;
  122. log.info("Changes collected. Number of changes: " + nNumberOfChanges);
  123. const result = [];
  124. if (nNumberOfChanges > 0) {
  125. changesContent = sortAndStringifyInFlexFormat(changesContent);
  126. result.push(createResource({
  127. path: `${pathPrefix}/changes/${bundleName}`,
  128. string: changesContent
  129. }));
  130. }
  131. return result;
  132. });
  133. }