Skip to content

Commit 104de97

Browse files
erikkempermanphated
authored andcommitted
Update: Revive wrap-vinyl as separate stream stage in src
1 parent d67ddf0 commit 104de97

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

lib/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var prepare = require('vinyl-prepare');
66
var toThrough = require('to-through');
77
var isValidGlob = require('is-valid-glob');
88

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

2223
var streams = [
2324
gs(glob, opt),
25+
wrapVinyl(opt),
2426
resolveSymlinks(opt),
2527
prepare.src(opt),
2628
readContents(opt),

lib/src/resolve-symlinks.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,30 @@ var boolean = valueOrFunction.boolean;
99

1010
function resolveSymlinks(opt) {
1111

12-
var resolveSymlinks = koalas(boolean(opt.resolveSymlinks), true);
13-
1412
// A stat property is exposed on file objects as a (wanted) side effect
15-
function resolveFile(globFile, enc, callback) {
16-
fs.lstat(globFile.path, onStat);
13+
function resolveFile(file, enc, callback) {
14+
15+
fs.lstat(file.path, onStat);
1716

1817
function onStat(statErr, stat) {
1918
if (statErr) {
2019
return callback(statErr);
2120
}
2221

23-
if (!stat.isSymbolicLink() || !resolveSymlinks) {
24-
globFile.stat = stat;
25-
return callback(null, globFile);
22+
file.stat = stat;
23+
24+
if (!stat.isSymbolicLink()) {
25+
return callback(null, file);
26+
}
27+
28+
var resolveSymlinks = koalas(boolean(opt.resolveSymlinks, file), true);
29+
30+
if (!resolveSymlinks) {
31+
return callback(null, file);
2632
}
2733

2834
// Recurse to get real file stat
29-
fs.stat(globFile.path, onStat);
35+
fs.stat(file.path, onStat);
3036
}
3137
}
3238

lib/src/wrap-vinyl.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
var File = require('vinyl');
4+
var through = require('through2');
5+
6+
function wrapVinyl(opt) {
7+
8+
function wrapFile(globFile, enc, callback) {
9+
10+
var file = new File(globFile);
11+
12+
callback(null, file);
13+
}
14+
15+
return through.obj(wrapFile);
16+
}
17+
18+
module.exports = wrapVinyl;

test/src-symlinks.js

+37
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,41 @@ describe('.src() with symlinks', function() {
126126
concat(assert),
127127
], done);
128128
});
129+
130+
it('recieves a file with symbolic link stats when resolveSymlinks is a function', function(done) {
131+
132+
function resolveSymlinks(file) {
133+
expect(file).toExist();
134+
expect(file.stat).toExist();
135+
expect(file.stat.isSymbolicLink()).toEqual(true);
136+
137+
return true;
138+
}
139+
140+
function assert(files) {
141+
expect(files.length).toEqual(1);
142+
// And the stats should have been updated
143+
expect(files[0].stat.isSymbolicLink()).toEqual(false);
144+
expect(files[0].stat.isFile()).toEqual(true);
145+
}
146+
147+
pipe([
148+
vfs.src(symlinkNestedFirst, { resolveSymlinks: resolveSymlinks }),
149+
concat(assert),
150+
], done);
151+
});
152+
153+
it('only calls resolveSymlinks once-per-file if it is a function', function(done) {
154+
155+
var spy = expect.createSpy().andReturn(true);
156+
157+
function assert() {
158+
expect(spy.calls.length).toEqual(1);
159+
}
160+
161+
pipe([
162+
vfs.src(symlinkNestedFirst, { resolveSymlinks: spy }),
163+
concat(assert),
164+
], done);
165+
});
129166
});

0 commit comments

Comments
 (0)