builder/lib/tasks/generateLibraryManifest.js

  1. "use strict";
  2. const log = require("@ui5/logger").getLogger("builder:tasks:generateLibraryManifest");
  3. const manifestCreator = require("../processors/manifestCreator");
  4. const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritized;
  5. /**
  6. * Task for creating a library manifest.json from its .library file.
  7. *
  8. * @public
  9. * @alias module:@ui5/builder.tasks.generateLibraryManifest
  10. * @param {object} parameters Parameters
  11. * @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
  12. * @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
  13. * @param {object} parameters.options Options
  14. * @param {string} parameters.options.projectName Project name
  15. * @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
  16. */
  17. module.exports = function({workspace, dependencies, options: {projectName}}) {
  18. const combo = new ReaderCollectionPrioritized({
  19. name: `libraryManifestGenerator - prioritize workspace over dependencies: ${projectName}`,
  20. readers: [workspace, dependencies]
  21. });
  22. // Note:
  23. // *.library files are needed to identify libraries
  24. // *.json files are needed to avoid overwriting them
  25. // *.js files are needed to identify nested components
  26. // *.less, *.css, *.theming and *.theme files are needed to identify supported themes
  27. // *.properties to identify existence of i18n bundles (e.g. messagebundle.properties)
  28. return combo.byGlob("/**/*.{js,json,library,less,css,theming,theme,properties}").then((resources) => {
  29. // Find all libraries and create a manifest.json file
  30. return workspace.byGlob("/resources/**/.library").then((libraryIndicatorResources) => {
  31. if (libraryIndicatorResources.length < 1) {
  32. // No library found - nothing to do
  33. log.verbose(`Could not find a ".library" file for project ${projectName}. ` +
  34. `Skipping library manifest generation.`);
  35. return;
  36. }
  37. return Promise.all(libraryIndicatorResources.map((libraryIndicatorResource) => {
  38. // Determine library namespace from library indicator file path
  39. // ending with ".library"
  40. // e.g. /resources/sap/foo/.library => sap/foo
  41. const libraryNamespacePattern = /^\/resources\/(.*)\/\.library$/;
  42. const libraryIndicatorPath = libraryIndicatorResource.getPath();
  43. const libraryNamespaceMatch = libraryIndicatorPath.match(libraryNamespacePattern);
  44. if (libraryNamespaceMatch && libraryNamespaceMatch[1]) {
  45. const libraryNamespace = libraryNamespaceMatch[1];
  46. return manifestCreator({
  47. libraryResource: libraryIndicatorResource,
  48. namespace: libraryNamespace,
  49. resources,
  50. options: {
  51. }
  52. }).then((manifest) => {
  53. if (manifest) {
  54. return workspace.write(manifest);
  55. }
  56. });
  57. } else {
  58. log.verbose(`Could not determine library namespace from file "${libraryIndicatorPath}" ` +
  59. `for project ${projectName}. Skipping library manifest generation.`);
  60. return Promise.resolve();
  61. }
  62. }));
  63. });
  64. });
  65. };