builder/lib/processors/debugFileCreator.js

  1. const copier = require("./resourceCopier");
  2. const util = require("util");
  3. /**
  4. * Creates *-dbg.js files for all supplied resources.
  5. *
  6. * @public
  7. * @alias module:@ui5/builder.processors.debugFileCreator
  8. * @param {object} parameters Parameters
  9. * @param {module:@ui5/fs.Resource[]} parameters.resources List of resources to be processed
  10. * @param {fs|module:@ui5/fs.fsInterface} parameters.fs Node fs or
  11. * custom [fs interface]{@link module:resources/module:@ui5/fs.fsInterface}
  12. * @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving with debug resources
  13. */
  14. module.exports = function({resources, fs}) {
  15. const options = {
  16. pattern: /((?:\.view|\.fragment|\.controller|\.designtime|\.support)?\.js)$/,
  17. replacement: "-dbg$1"
  18. };
  19. const stat = util.promisify(fs.stat);
  20. return Promise.all(
  21. resources.map((resource) => {
  22. // check whether the debug resource path is already used in the
  23. // previous tasks
  24. return stat(resource.getPath().replace(options.pattern, options.replacement))
  25. .then(
  26. // if the file can be found, it should be filtered out from creating debug file
  27. () => false,
  28. (err) => {
  29. if (err.code === "ENOENT") {
  30. // if the file can't be found, it should be included in creating debug file
  31. return resource;
  32. }
  33. // if it's other error, forward it
  34. throw err;
  35. }
  36. );
  37. })
  38. ).then((results) => {
  39. // filter out the resouces whose debug source path is already used
  40. return results.filter((result) => {
  41. return !!result;
  42. });
  43. }).then((filteredResources) => {
  44. return copier({
  45. resources: filteredResources,
  46. options: options
  47. });
  48. });
  49. };