index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var fs = require('fs');
  3. var writeDir = require('./writeDir');
  4. var writeStream = require('./writeStream');
  5. var writeBuffer = require('./writeBuffer');
  6. function writeContents(writePath, file, cb) {
  7. var written = function(err) {
  8. var done = function(err) {
  9. cb(err, file);
  10. };
  11. if (err) {
  12. return done(err);
  13. }
  14. if (!file.stat || typeof file.stat.mode !== 'number') {
  15. return done();
  16. }
  17. fs.stat(writePath, function(err, st) {
  18. if (err) {
  19. return done(err);
  20. }
  21. // octal 7777 = decimal 4095
  22. var currentMode = (st.mode & 4095);
  23. if (currentMode === file.stat.mode) {
  24. return done();
  25. }
  26. fs.chmod(writePath, file.stat.mode, done);
  27. });
  28. };
  29. // if directory then mkdirp it
  30. if (file.isDirectory()) {
  31. writeDir(writePath, file, written);
  32. return;
  33. }
  34. // stream it to disk yo
  35. if (file.isStream()) {
  36. writeStream(writePath, file, written);
  37. return;
  38. }
  39. // write it like normal
  40. if (file.isBuffer()) {
  41. writeBuffer(writePath, file, written);
  42. return;
  43. }
  44. // if no contents then do nothing
  45. if (file.isNull()) {
  46. cb(null, file);
  47. return;
  48. }
  49. }
  50. module.exports = writeContents;