copy-utils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const path = require("path");
  2. /**
  3. * Takes in a file path in the `./templates` directory. Copies that
  4. * file to the destination, with the `.tpl` extension stripped.
  5. *
  6. * @param {Generator} generator A Yeoman Generator instance
  7. * @param {string} templateDir Absolute path to template directory
  8. * @returns {Function} A curried function that takes a file path and copies it
  9. */
  10. const generatorCopy = (
  11. generator,
  12. templateDir
  13. ) => /** @param {string} filePath */ filePath => {
  14. const sourceParts = templateDir.split(path.delimiter);
  15. sourceParts.push.apply(sourceParts, filePath.split("/"));
  16. const targetParts = path.dirname(filePath).split("/");
  17. targetParts.push(path.basename(filePath, ".tpl"));
  18. generator.fs.copy(
  19. path.join.apply(null, sourceParts),
  20. generator.destinationPath(path.join.apply(null, targetParts))
  21. );
  22. };
  23. /**
  24. * Takes in a file path in the `./templates` directory. Copies that
  25. * file to the destination, with the `.tpl` extension and `_` prefix
  26. * stripped. Passes `this.props` to the template.
  27. *
  28. * @param {Generator} generator A Yeoman Generator instance
  29. * @param {string} templateDir Absolute path to template directory
  30. * @param {any} templateData An object containing the data passed to
  31. * the template files.
  32. * @returns {Function} A curried function that takes a file path and copies it
  33. */
  34. const generatorCopyTpl = (
  35. generator,
  36. templateDir,
  37. templateData
  38. ) => /** @param {string} filePath */ filePath => {
  39. const sourceParts = templateDir.split(path.delimiter);
  40. sourceParts.push.apply(sourceParts, filePath.split("/"));
  41. const targetParts = path.dirname(filePath).split("/");
  42. targetParts.push(path.basename(filePath, ".tpl").slice(1));
  43. generator.fs.copyTpl(
  44. path.join.apply(null, sourceParts),
  45. generator.destinationPath(path.join.apply(null, targetParts)),
  46. templateData
  47. );
  48. };
  49. module.exports = {
  50. generatorCopy,
  51. generatorCopyTpl
  52. };