Skip to content

Commit

Permalink
Follow symlinks
Browse files Browse the repository at this point in the history
* Removed `getStats` function.
* Added a `resolveSymlinks` function that exposes a `stat` property on
  file object as a side effect (making `getStats` obsolete).
* Added tests.
* Fixes #39.
  • Loading branch information
valeriangalliat authored and phated committed Nov 28, 2017
1 parent 62c981d commit d633a60
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
19 changes: 0 additions & 19 deletions lib/src/getStats.js

This file was deleted.

6 changes: 3 additions & 3 deletions lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var merge = require('merge-stream');

var filterSince = require('./filterSince');
var getContents = require('./getContents');
var getStats = require('./getStats');
var resolveSymlinks = require('./resolveSymlinks');

function createFile(globFile, enc, cb) {
cb(null, new File(globFile));
Expand Down Expand Up @@ -37,8 +37,8 @@ function src(glob, opt) {
var globStream = gs.create(glob, options);

var outputStream = globStream
.pipe(through.obj(createFile))
.pipe(getStats(options));
.pipe(resolveSymlinks())
.pipe(through.obj(createFile));

if (options.since) {
outputStream = outputStream
Expand Down
38 changes: 38 additions & 0 deletions lib/src/resolveSymlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

var through2 = require('through2');
var fs = require('graceful-fs');
var path = require('path');

function resolveSymlinks() {
return through2.obj(resolveFile);
}

// 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()) {
return cb(null, globFile);
}

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

globFile.base = path.dirname(filePath);
globFile.path = filePath;

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

module.exports = resolveSymlinks;
1 change: 1 addition & 0 deletions test/fixtures/test-symlink
1 change: 1 addition & 0 deletions test/fixtures/test-symlink-dir
20 changes: 20 additions & 0 deletions test/src.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,24 @@ describe('source stream', function() {
stream1.pipe(stream2).pipe(bufferStream);
});

it('should follow file symlinks', function(done) {
var expectedPath = path.join(__dirname, './fixtures/test.coffee');

var stream = vfs.src('./fixtures/test-symlink', {cwd: __dirname});
stream.on('data', function(file){
file.path.should.equal(expectedPath);
done();
});
});

it('should follow dir symlinks', function(done) {
var expectedPath = path.join(__dirname, './fixtures/wow');

var stream = vfs.src('./fixtures/test-symlink-dir', {cwd: __dirname});
stream.on('data', function(file){
file.path.should.equal(expectedPath);
done();
});
});

});

0 comments on commit d633a60

Please sign in to comment.