fileNameFormatter.js 964 B

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