Skip to content

Commit

Permalink
Update: Revive wrap-vinyl as separate stream stage in src
Browse files Browse the repository at this point in the history
  • Loading branch information
erikkemperman authored and phated committed Nov 30, 2017
1 parent 33ee92a commit 8cd5aff
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var prepare = require('vinyl-prepare');
var toThrough = require('to-through');
var isValidGlob = require('is-valid-glob');

var wrapVinyl = require('./wrap-vinyl');
var sourcemap = require('./sourcemap');
var readContents = require('./read-contents');
var resolveSymlinks = require('./resolve-symlinks');
Expand All @@ -21,6 +22,7 @@ function src(glob, opt) {

var streams = [
gs(glob, opt),
wrapVinyl(opt),
resolveSymlinks(opt),
prepare.src(opt),
readContents(opt),
Expand Down
22 changes: 14 additions & 8 deletions lib/src/resolve-symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,30 @@ var boolean = valueOrFunction.boolean;

function resolveSymlinks(opt) {

var resolveSymlinks = koalas(boolean(opt.resolveSymlinks), true);

// A stat property is exposed on file objects as a (wanted) side effect
function resolveFile(globFile, enc, callback) {
fs.lstat(globFile.path, onStat);
function resolveFile(file, enc, callback) {

fs.lstat(file.path, onStat);

function onStat(statErr, stat) {
if (statErr) {
return callback(statErr);
}

if (!stat.isSymbolicLink() || !resolveSymlinks) {
globFile.stat = stat;
return callback(null, globFile);
file.stat = stat;

if (!stat.isSymbolicLink()) {
return callback(null, file);
}

var resolveSymlinks = koalas(boolean(opt.resolveSymlinks, file), true);

if (!resolveSymlinks) {
return callback(null, file);
}

// Recurse to get real file stat
fs.stat(globFile.path, onStat);
fs.stat(file.path, onStat);
}
}

Expand Down
18 changes: 18 additions & 0 deletions lib/src/wrap-vinyl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

var File = require('vinyl');
var through = require('through2');

function wrapVinyl(opt) {

function wrapFile(globFile, enc, callback) {

var file = new File(globFile);

callback(null, file);
}

return through.obj(wrapFile);
}

module.exports = wrapVinyl;
37 changes: 37 additions & 0 deletions test/src-symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,41 @@ describe('.src() with symlinks', function() {
concat(assert),
], done);
});

it('recieves a file with symbolic link stats when resolveSymlinks is a function', function(done) {

function resolveSymlinks(file) {
expect(file).toExist();
expect(file.stat).toExist();
expect(file.stat.isSymbolicLink()).toEqual(true);

return true;
}

function assert(files) {
expect(files.length).toEqual(1);
// And the stats should have been updated
expect(files[0].stat.isSymbolicLink()).toEqual(false);
expect(files[0].stat.isFile()).toEqual(true);
}

pipe([
vfs.src(symlinkNestedFirst, { resolveSymlinks: resolveSymlinks }),
concat(assert),
], done);
});

it('only calls resolveSymlinks once-per-file if it is a function', function(done) {

var spy = expect.createSpy().andReturn(true);

function assert() {
expect(spy.calls.length).toEqual(1);
}

pipe([
vfs.src(symlinkNestedFirst, { resolveSymlinks: spy }),
concat(assert),
], done);
});
});

0 comments on commit 8cd5aff

Please sign in to comment.