builder/lib/tasks/replaceBuildtime.js

  1. const stringReplacer = require("../processors/stringReplacer");
  2. function pad(v) {
  3. return String(v).padStart(2, "0");
  4. }
  5. function getTimestamp() {
  6. const date = new Date();
  7. const year = date.getFullYear();
  8. const month = pad(date.getMonth() + 1);
  9. const day = pad(date.getDate());
  10. const hours = pad(date.getHours());
  11. const minutes = pad(date.getMinutes());
  12. // yyyyMMdd-HHmm
  13. return year + month + day + "-" + hours + minutes;
  14. }
  15. /**
  16. * Task to replace the buildtime <code>${buildtime}</code>.
  17. *
  18. * @public
  19. * @alias module:@ui5/builder.tasks.replaceBuildtime
  20. * @param {object} parameters Parameters
  21. * @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
  22. * @param {object} parameters.options Options
  23. * @param {string} parameters.options.pattern Pattern to locate the files to be processed
  24. * @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
  25. */
  26. module.exports = function({workspace, options: {pattern}}) {
  27. const timestamp = getTimestamp();
  28. return workspace.byGlob(pattern)
  29. .then((processedResources) => {
  30. return stringReplacer({
  31. resources: processedResources,
  32. options: {
  33. pattern: "${buildtime}",
  34. replacement: timestamp
  35. }
  36. });
  37. })
  38. .then((processedResources) => {
  39. return Promise.all(processedResources.map((resource) => {
  40. return workspace.write(resource);
  41. }));
  42. });
  43. };