prepare.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var path = require('path');
  3. var fs = require('graceful-fs');
  4. var Vinyl = require('vinyl');
  5. var through = require('through2');
  6. function prepareWrite(folderResolver, optResolver) {
  7. if (!folderResolver) {
  8. throw new Error('Invalid output folder');
  9. }
  10. function normalize(file, enc, cb) {
  11. if (!Vinyl.isVinyl(file)) {
  12. return cb(new Error('Received a non-Vinyl object in `dest()`'));
  13. }
  14. // TODO: Remove this after people upgrade vinyl/transition from gulp-util
  15. if (typeof file.isSymbolic !== 'function') {
  16. file = new Vinyl(file);
  17. }
  18. var outFolderPath = folderResolver.resolve('outFolder', file);
  19. if (!outFolderPath) {
  20. return cb(new Error('Invalid output folder'));
  21. }
  22. var cwd = path.resolve(optResolver.resolve('cwd', file));
  23. var basePath = path.resolve(cwd, outFolderPath);
  24. var writePath = path.resolve(basePath, file.relative);
  25. // Wire up new properties
  26. file.cwd = cwd;
  27. file.base = basePath;
  28. file.path = writePath;
  29. if (!file.isSymbolic()) {
  30. var mode = optResolver.resolve('mode', file);
  31. file.stat = (file.stat || new fs.Stats());
  32. file.stat.mode = mode;
  33. }
  34. cb(null, file);
  35. }
  36. return through.obj(normalize);
  37. }
  38. module.exports = prepareWrite;