index.js 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. var through = require('through2');
  3. var mkdirp = require('./mkdirp');
  4. function toFunction(dirpath) {
  5. function stringResolver(chunk, callback) {
  6. callback(null, dirpath);
  7. }
  8. return stringResolver;
  9. }
  10. function define(options) {
  11. function mkdirpStream(resolver) {
  12. // Handle resolver that's just a dirpath
  13. if (typeof resolver === 'string') {
  14. resolver = toFunction(resolver);
  15. }
  16. function makeFileDirs(chunk, enc, callback) {
  17. resolver(chunk, onDirpath);
  18. function onDirpath(dirpathErr, dirpath, mode) {
  19. if (dirpathErr) {
  20. return callback(dirpathErr);
  21. }
  22. mkdirp(dirpath, mode, onMkdirp);
  23. }
  24. function onMkdirp(mkdirpErr) {
  25. if (mkdirpErr) {
  26. return callback(mkdirpErr);
  27. }
  28. callback(null, chunk);
  29. }
  30. }
  31. return through(options, makeFileDirs);
  32. }
  33. return mkdirpStream;
  34. }
  35. module.exports = define();
  36. module.exports.obj = define({ objectMode: true, highWaterMark: 16 });