builder/lib/tasks/replaceCopyright.js

  1. const stringReplacer = require("../processors/stringReplacer");
  2. /**
  3. * Task to to replace the copyright.
  4. *
  5. * The following placeholders are replaced with corresponding values:
  6. * <ul>
  7. * <li>${copyright}</li>
  8. * <li>@copyright@</li>
  9. * </ul>
  10. *
  11. * If the copyright string contains the optional placeholder ${currentYear}
  12. * it will be replaced with the current year.
  13. * If no copyright string is given, no replacement is being done.
  14. *
  15. * @public
  16. * @alias module:@ui5/builder.tasks.replaceCopyright
  17. * @param {object} parameters Parameters
  18. * @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
  19. * @param {object} parameters.options Options
  20. * @param {string} parameters.options.copyright Replacement copyright
  21. * @param {string} parameters.options.pattern Pattern to locate the files to be processed
  22. * @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
  23. */
  24. module.exports = function({workspace, options: {copyright, pattern}}) {
  25. if (!copyright) {
  26. return Promise.resolve();
  27. }
  28. // Replace optional placeholder ${currentYear} with the current year
  29. copyright = copyright.replace(/(?:\$\{currentYear\})/, new Date().getFullYear());
  30. return workspace.byGlob(pattern)
  31. .then((processedResources) => {
  32. return stringReplacer({
  33. resources: processedResources,
  34. options: {
  35. pattern: /(?:\$\{copyright\}|@copyright@)/g,
  36. replacement: copyright
  37. }
  38. });
  39. })
  40. .then((processedResources) => {
  41. return Promise.all(processedResources.map((resource) => {
  42. return workspace.write(resource);
  43. }));
  44. });
  45. };