fileNameFormatter.js 883 B

12345678910111213141516171819202122232425262728293031323334353637
  1. const debug = require("debug")("streamroller:fileNameFormatter");
  2. const path = require("path");
  3. const FILENAME_SEP = ".";
  4. const ZIP_EXT = ".gz";
  5. module.exports = ({
  6. file,
  7. keepFileExt,
  8. needsIndex,
  9. alwaysIncludeDate,
  10. compress
  11. }) => {
  12. const dirAndName = path.join(file.dir, file.name);
  13. const ext = f => f + file.ext;
  14. const index = (f, i, d) =>
  15. (needsIndex || !d) && i ? f + FILENAME_SEP + i : f;
  16. const date = (f, i, d) => {
  17. return (i > 0 || alwaysIncludeDate) && d ? f + FILENAME_SEP + d : f;
  18. };
  19. const gzip = (f, i) => (i && compress ? f + ZIP_EXT : f);
  20. const parts = keepFileExt
  21. ? [date, index, ext, gzip]
  22. : [ext, date, index, gzip];
  23. return ({ date, index }) => {
  24. debug(`_formatFileName: date=${date}, index=${index}`);
  25. return parts.reduce(
  26. (filename, part) => part(filename, index, date),
  27. dirAndName
  28. );
  29. };
  30. };