index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. var through = require('through2');
  3. var writeDir = require('./write-dir');
  4. var writeStream = require('./write-stream');
  5. var writeBuffer = require('./write-buffer');
  6. var writeSymbolicLink = require('./write-symbolic-link');
  7. var fo = require('../../file-operations');
  8. function writeContents(optResolver) {
  9. function writeFile(file, enc, callback) {
  10. // Write it as a symlink
  11. if (file.isSymbolic()) {
  12. return writeSymbolicLink(file, optResolver, onWritten);
  13. }
  14. // If directory then mkdirp it
  15. if (file.isDirectory()) {
  16. return writeDir(file, optResolver, onWritten);
  17. }
  18. // Stream it to disk yo
  19. if (file.isStream()) {
  20. return writeStream(file, optResolver, onWritten);
  21. }
  22. // Write it like normal
  23. if (file.isBuffer()) {
  24. return writeBuffer(file, optResolver, onWritten);
  25. }
  26. // If no contents then do nothing
  27. if (file.isNull()) {
  28. return onWritten();
  29. }
  30. // This is invoked by the various writeXxx modules when they've finished
  31. // writing the contents.
  32. function onWritten(writeErr) {
  33. var flags = fo.getFlags({
  34. overwrite: optResolver.resolve('overwrite', file),
  35. append: optResolver.resolve('append', file),
  36. });
  37. if (fo.isFatalOverwriteError(writeErr, flags)) {
  38. return callback(writeErr);
  39. }
  40. callback(null, file);
  41. }
  42. }
  43. return through.obj(writeFile);
  44. }
  45. module.exports = writeContents;