index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var map = require("map-stream"),
  2. Path = require("path");
  3. module.exports = function (obj) {
  4. "use strict";
  5. function parsePath(path) {
  6. var extname = Path.extname(path);
  7. return {
  8. dirname: Path.dirname(path),
  9. basename: Path.basename(path, extname),
  10. extname: extname
  11. };
  12. }
  13. function rename(file, callback) {
  14. var parsedPath = parsePath(file.relative);
  15. var path;
  16. var type = typeof obj;
  17. if (type === "string" && obj !== "") {
  18. path = obj;
  19. } else if (type === "function") {
  20. var result = obj(parsedPath) || parsedPath;
  21. path = Path.join(result.dirname, result.basename + result.extname);
  22. } else if (type === "object" && obj !== undefined && obj !== null) {
  23. var dirname = 'dirname' in obj ? obj.dirname : parsedPath.dirname,
  24. prefix = obj.prefix || "",
  25. suffix = obj.suffix || "",
  26. basename = 'basename' in obj ? obj.basename : parsedPath.basename,
  27. extname = 'extname' in obj ? obj.extname : parsedPath.extname;
  28. path = Path.join(dirname, prefix + basename + suffix + extname);
  29. } else {
  30. callback(new Error("Unsupported renaming parameter type supplied"), undefined);
  31. return;
  32. }
  33. file.path = Path.join(file.base, path);
  34. callback(null, file);
  35. }
  36. return map(rename);
  37. };