From bcfd3423d36059143960a6d08fda88673bc383bd Mon Sep 17 00:00:00 2001 From: Vineet Hawal Date: Thu, 1 Oct 2015 15:26:06 +0530 Subject: [PATCH] New: Pass options to through2 instances --- README.md | 11 +++++++++-- lib/dest/index.js | 3 +-- lib/src/getContents/index.js | 2 +- lib/src/index.js | 6 +++--- lib/src/resolveSymlinks.js | 2 +- lib/symlink/index.js | 2 +- test/dest.js | 21 +++++++++++++++++++++ test/symlink.js | 21 +++++++++++++++++++++ 8 files changed, 58 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a724ddc6..98415ad0 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,8 @@ fs.src(['*.js', '!b*.js']) - `false` will make `file.symlink` equal the original symlink's target path. - Any glob-related options are documented in [glob-stream] and [node-glob]. + - Any through2-related options are documented in [through2] + - Returns a Readable stream by default, or a Duplex stream if the `passthrough` option is set to `true`. - This stream emits matching [vinyl] File objects. @@ -107,6 +109,8 @@ _Note:_ UTF-8 BOM will be stripped from all UTF-8 files read with `.src`. - Any other options are passed through to `gulp-sourcemaps` - fs.dest('./', {
sourcemaps: {
path: '.',
addComment: false,
includeContent: false
}
}) + - Any through2-related options are documented in [through2] + - Returns a Readable/Writable stream. - On write the stream will save the [vinyl] File to disk at the folder/cwd specified. - After writing the file to disk, it will be emitted from the stream so you can keep piping these around. @@ -129,6 +133,8 @@ _Note:_ UTF-8 BOM will be stripped from all UTF-8 files read with `.src`. - dirMode - Specify the mode the directory should be created with. - Default is the process mode. + - Any through2-related options are documented in [through2] + - Returns a Readable/Writable stream. - On write the stream will create a symbolic link (i.e. symlink) on disk at the folder/cwd specified. - After creating the symbolic link, it will be emitted from the stream so you can keep piping these around. @@ -138,8 +144,9 @@ _Note:_ UTF-8 BOM will be stripped from all UTF-8 files read with `.src`. [glob-stream]: https://github.com/gulpjs/glob-stream [node-glob]: https://github.com/isaacs/node-glob [gaze]: https://github.com/shama/gaze -[glob-watcher]: https://github.com/gulpjs/glob-watcher -[vinyl]: https://github.com/gulpjs/vinyl +[glob-watcher]: https://github.com/wearefractal/glob-watcher +[vinyl]: https://github.com/wearefractal/vinyl +[through2]: https://github.com/rvagg/through2 [npm-url]: https://www.npmjs.com/package/vinyl-fs [npm-image]: https://badge.fury.io/js/vinyl-fs.svg [travis-url]: https://travis-ci.org/gulpjs/vinyl-fs diff --git a/lib/dest/index.js b/lib/dest/index.js index 81b5417a..1393621c 100644 --- a/lib/dest/index.js +++ b/lib/dest/index.js @@ -21,8 +21,7 @@ function dest(outFolder, opt) { }); } - var saveStream = through2.obj(saveFile); - + var saveStream = through2.obj(opt, saveFile); if (!opt.sourcemaps) { // Sink the save stream to start flowing // Do this on nextTick, it will flow at slowest speed of piped streams diff --git a/lib/src/getContents/index.js b/lib/src/getContents/index.js index 53d51302..87fa3120 100644 --- a/lib/src/getContents/index.js +++ b/lib/src/getContents/index.js @@ -7,7 +7,7 @@ var bufferFile = require('./bufferFile'); var streamFile = require('./streamFile'); function getContents(opt) { - return through2.obj(function(file, enc, cb) { + return through2.obj(opt, function(file, enc, cb) { // don't fail to read a directory if (file.isDirectory()) { return readDir(file, opt, cb); diff --git a/lib/src/index.js b/lib/src/index.js index a1341b91..b14ba473 100644 --- a/lib/src/index.js +++ b/lib/src/index.js @@ -1,7 +1,7 @@ 'use strict'; var assign = require('object-assign'); -var through = require('through2'); +var through2 = require('through2'); var gs = require('glob-stream'); var File = require('vinyl'); var duplexify = require('duplexify'); @@ -37,7 +37,7 @@ function src(glob, opt) { var outputStream = globStream .pipe(resolveSymlinks(options)) - .pipe(through.obj(createFile)); + .pipe(through2.obj(opt, createFile)); if (options.since != null) { outputStream = outputStream @@ -50,7 +50,7 @@ function src(glob, opt) { } if (options.passthrough === true) { - inputPass = through.obj(); + inputPass = through2.obj(opt); outputStream = duplexify.obj(inputPass, merge(outputStream, inputPass)); } if (options.sourcemaps === true) { diff --git a/lib/src/resolveSymlinks.js b/lib/src/resolveSymlinks.js index 450d691c..0b36c6c7 100644 --- a/lib/src/resolveSymlinks.js +++ b/lib/src/resolveSymlinks.js @@ -33,7 +33,7 @@ function resolveSymlinks(options) { }); } - return through2.obj(resolveFile); + return through2.obj(options, resolveFile); } module.exports = resolveSymlinks; diff --git a/lib/symlink/index.js b/lib/symlink/index.js index c77b363b..6c01a6b6 100644 --- a/lib/symlink/index.js +++ b/lib/symlink/index.js @@ -21,7 +21,7 @@ function symlink(outFolder, opt) { }); } - var stream = through2.obj(linkFile); + var stream = through2.obj(opt, linkFile); // TODO: option for either backpressure or lossy stream.resume(); return stream; diff --git a/test/dest.js b/test/dest.js index f76fc12b..c24c0394 100644 --- a/test/dest.js +++ b/test/dest.js @@ -1326,4 +1326,25 @@ describe('dest stream', function() { .pipe(destStream) .pipe(slowCountFiles); }); + + it('should pass options to through2',function(done){ + var srcPath = path.join(__dirname, './fixtures/test.coffee'); + var content = fs.readFileSync(srcPath); + var stream = vfs.dest('./out-fixtures/', {cwd: __dirname, objectMode: false}); + + stream.on('error', function(err){ + err.should.match(/Invalid non-string\/buffer chunk/); + done() + }); + + var file = new File({ + path: srcPath, + cwd: __dirname, + contents: content + }) + + stream.write(file); + stream.end(); + }); + }); diff --git a/test/symlink.js b/test/symlink.js index 844f24a6..30b4ab6d 100644 --- a/test/symlink.js +++ b/test/symlink.js @@ -415,4 +415,25 @@ describe('symlink stream', function() { stream.end(); }); }); + + it('should pass options to through2',function(done){ + var srcPath = path.join(__dirname, './fixtures/test.coffee'); + var content = fs.readFileSync(srcPath); + var stream = vfs.symlink('./out-fixtures/', {cwd: __dirname, objectMode: false}); + + stream.on('error', function(err){ + err.should.match(/Invalid non-string\/buffer chunk/); + done() + }); + + var file = new File({ + path: srcPath, + cwd: __dirname, + contents: content + }) + + stream.write(file); + stream.end(); + }); + });