prepare.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 prepareSymlink(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 `symlink()`'));
  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 cwd = path.resolve(optResolver.resolve('cwd', file));
  19. var outFolderPath = folderResolver.resolve('outFolder', file);
  20. if (!outFolderPath) {
  21. return cb(new Error('Invalid output folder'));
  22. }
  23. var basePath = path.resolve(cwd, outFolderPath);
  24. var writePath = path.resolve(basePath, file.relative);
  25. // Wire up new properties
  26. // Note: keep the target stats for now, we may need them in link-file
  27. file.stat = (file.stat || new fs.Stats());
  28. file.cwd = cwd;
  29. file.base = basePath;
  30. // This is the path we are linking *TO*
  31. file.symlink = file.path;
  32. file.path = writePath;
  33. // We have to set contents to null for a link
  34. // Otherwise `isSymbolic()` returns false
  35. file.contents = null;
  36. cb(null, file);
  37. }
  38. return through.obj(normalize);
  39. }
  40. module.exports = prepareSymlink;