file.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. var _ = require('lodash'),
  3. fs = require('fs-extra'),
  4. glob = require('glob'),
  5. path = require('path');
  6. var minify = require('../common/minify.js');
  7. /*----------------------------------------------------------------------------*/
  8. /**
  9. * Creates a [fs.copy](https://github.com/jprichardson/node-fs-extra#copy)
  10. * function with `srcPath` and `destPath` partially applied.
  11. *
  12. * @memberOf file
  13. * @param {string} srcPath The path of the file to copy.
  14. * @param {string} destPath The path to copy the file to.
  15. * @returns {Function} Returns the partially applied function.
  16. */
  17. function copy(srcPath, destPath) {
  18. return _.partial(fs.copy, srcPath, destPath);
  19. }
  20. /**
  21. * Creates an object of base name and compiled template pairs that match `pattern`.
  22. *
  23. * @memberOf file
  24. * @param {string} pattern The glob pattern to be match.
  25. * @returns {Object} Returns the object of compiled templates.
  26. */
  27. function globTemplate(pattern) {
  28. return _.transform(glob.sync(pattern), function(result, filePath) {
  29. var key = path.basename(filePath, path.extname(filePath));
  30. result[key] = _.template(fs.readFileSync(filePath, 'utf8'));
  31. }, {});
  32. }
  33. /**
  34. * Creates a `minify` function with `srcPath` and `destPath` partially applied.
  35. *
  36. * @memberOf file
  37. * @param {string} srcPath The path of the file to minify.
  38. * @param {string} destPath The path to write the file to.
  39. * @returns {Function} Returns the partially applied function.
  40. */
  41. function min(srcPath, destPath) {
  42. return _.partial(minify, srcPath, destPath);
  43. }
  44. /**
  45. * Creates a [fs.writeFile](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback)
  46. * function with `filePath` and `data` partially applied.
  47. *
  48. * @memberOf file
  49. * @param {string} destPath The path to write the file to.
  50. * @param {string} data The data to write to the file.
  51. * @returns {Function} Returns the partially applied function.
  52. */
  53. function write(destPath, data) {
  54. return _.partial(fs.writeFile, destPath, data);
  55. }
  56. /*----------------------------------------------------------------------------*/
  57. module.exports = {
  58. 'copy': copy,
  59. 'globTemplate': globTemplate,
  60. 'min': min,
  61. 'write': write
  62. };