index.js 754 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var through2 = require('through2');
  3. var fs = require('graceful-fs');
  4. var prepareWrite = require('../prepareWrite');
  5. function symlink(outFolder, opt) {
  6. function linkFile(file, enc, cb) {
  7. var srcPath = file.path;
  8. var symType = (file.isDirectory() ? 'dir' : 'file');
  9. prepareWrite(outFolder, file, opt, function(err, writePath) {
  10. if (err) {
  11. return cb(err);
  12. }
  13. fs.symlink(srcPath, writePath, symType, function(err) {
  14. if (err && err.code !== 'EEXIST') {
  15. return cb(err);
  16. }
  17. cb(null, file);
  18. });
  19. });
  20. }
  21. var stream = through2.obj(opt, linkFile);
  22. // TODO: option for either backpressure or lossy
  23. stream.resume();
  24. return stream;
  25. }
  26. module.exports = symlink;