aboutsummaryrefslogtreecommitdiff
path: root/node_modules/vinyl-fs/lib/src/wrapWithVinylFile.js
blob: 5e6290e59da99c98a6aea3cc5cbe8f784b096d2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';

var through2 = require('through2');
var fs = require('graceful-fs');
var File = require('vinyl');

function wrapWithVinylFile(options) {

  // A stat property is exposed on file objects as a (wanted) side effect
  function resolveFile(globFile, enc, cb) {
    fs.lstat(globFile.path, function(err, stat) {
      if (err) {
        return cb(err);
      }

      globFile.stat = stat;

      if (!stat.isSymbolicLink() || !options.followSymlinks) {
        var vinylFile = new File(globFile);
        if (globFile.originalSymlinkPath) {
          // If we reach here, it means there is at least one
          // symlink on the path and we need to rewrite the path
          // to its original value.
          // Updated file stats will tell getContents() to actually read it.
          vinylFile.path = globFile.originalSymlinkPath;
        }
        return cb(null, vinylFile);
      }

      fs.realpath(globFile.path, function(err, filePath) {
        if (err) {
          return cb(err);
        }

        if (!globFile.originalSymlinkPath) {
          // Store the original symlink path before the recursive call
          // to later rewrite it back.
          globFile.originalSymlinkPath = globFile.path;
        }
        globFile.path = filePath;

        // Recurse to get real file stat
        resolveFile(globFile, enc, cb);
      });
    });
  }

  return through2.obj(options, resolveFile);
}

module.exports = wrapWithVinylFile;