builder/lib/processors/bundlers/flexChangesBundler.js

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