builder/lib/tasks/jsdoc/executeJsdocSdkTransformation.js

  1. const log = require("@ui5/logger").getLogger("builder:tasks:jsdoc:executeJsdocSdkTransformation");
  2. const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritized;
  3. const fsInterface = require("@ui5/fs").fsInterface;
  4. const sdkTransformer = require("../../processors/jsdoc/sdkTransformer");
  5. /**
  6. * Task to transform the api.json file as created by the
  7. * [generateJsdoc]{@link module:@ui5/builder.tasks.generateJsdoc} task into a pre-processed api.json
  8. * file suitable for the SDK.
  9. *
  10. * @public
  11. * @alias module:@ui5/builder.tasks.executeJsdocSdkTransformation
  12. * @param {object} parameters Parameters
  13. * @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
  14. * @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
  15. * @param {object} parameters.options Options
  16. * @param {string|Array} parameters.options.dotLibraryPattern Pattern to locate the .library resource to be processed
  17. * @param {string} parameters.options.projectName Project name
  18. * @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
  19. */
  20. const executeJsdocSdkTransformation = async function(
  21. {workspace, dependencies, options: {projectName, dotLibraryPattern}} = {}
  22. ) {
  23. if (!projectName || !dotLibraryPattern) {
  24. throw new Error("[executeJsdocSdkTransformation]: One or more mandatory options not provided");
  25. }
  26. const [apiJsons, dotLibraries, depApiJsons] = await Promise.all([
  27. workspace.byGlob("/test-resources/**/designtime/api.json"),
  28. workspace.byGlob(dotLibraryPattern),
  29. dependencies.byGlob("/test-resources/**/designtime/api.json")
  30. ]);
  31. if (!apiJsons.length) {
  32. log.info(`Failed to locate api.json resource for project ${projectName}. ` +
  33. `Skipping SDK Transformation...`);
  34. return;
  35. } else if (apiJsons.length > 1) {
  36. throw new Error(`[executeJsdocSdkTransformation]: Found more than one api.json resources for project ` +
  37. `${projectName}.`);
  38. }
  39. if (!dotLibraries.length) {
  40. throw new Error(`[executeJsdocSdkTransformation]: Failed to locate .library resource for project ` +
  41. `${projectName}.`);
  42. } else if (dotLibraries.length > 1) {
  43. throw new Error(`[executeJsdocSdkTransformation]: Found more than one .library resources for project ` +
  44. `${projectName}.`);
  45. }
  46. const combo = new ReaderCollectionPrioritized({
  47. name: `executeJsdocSdkTransformation - custom workspace + dependencies FS: ${projectName}`,
  48. readers: [workspace, dependencies]
  49. });
  50. const apiJsonPath = apiJsons[0].getPath();
  51. const dotLibraryPath = dotLibraries[0].getPath();
  52. const dependencyApiJsonPaths = depApiJsons.map((res) => {
  53. return res.getPath();
  54. });
  55. // Target path is typically "/test-resources/${options.namespace}/designtime/apiref/api.json"
  56. const targetApiJsonPath = apiJsonPath.replace(/\/api\.json$/i, "/apiref/api.json");
  57. const createdResources = await sdkTransformer({
  58. apiJsonPath,
  59. dotLibraryPath,
  60. dependencyApiJsonPaths,
  61. targetApiJsonPath,
  62. fs: fsInterface(combo)
  63. });
  64. await Promise.all(createdResources.map((resource) => {
  65. return workspace.write(resource);
  66. }));
  67. };
  68. module.exports = executeJsdocSdkTransformation;