read vinyl file stream
Turns out that reading all the files in a vinyl stream is cumbersome, and supporting all of the options is a little bit annoying. I decided that I don't want to write that code more than once. So here is a library that does that. This is most useful for gulp plugins that need to transform all the files in a stream, though I am sure you can figure out other ways to use it too.
Install
npm install read-vinyl-file-stream
API
read-vinyl-file-stream(iterator {Function} [, flush {Function}] [, encoding {String}])
The module is a function that creates a transform stream. It will read the vinyl file, whether it is a buffer or a stream internally. It takes the following parameters, in order:
- iterator {Function} Required - the method that will process the files.
- flush {Function} Optional - the method to call before the stream ends.
- encoding {String} Optional - the encoding to use for the content provided to the iterator function. By default, this is a UTF-8 string. The following options are supported:
'utf8'
- provide the content in a UTF-8 string.'buffer'
- provide the content in a raw buffer. This is useful if you are processing binary files, for example.
iterator(content, file, stream, cb)
The function that you provide to it has the following parameters, in order:
- content - the content of the file.
- file - the vinyl file itself.
- stream - the transform stream that is being iterated.
- cb - a callback to call once you are done processing the file. You must call this in order for the stream to continue.
flush(stream, cb)
This is a function that will allow you to execute some code after all the files have been read but before the stream ends. It has the following parameters, in order:
- stream - the transform stream that is being iterated.
- cb - a callback to call once you are done with the flush actions. You must call this in order for the stream to end.
Examples
Observe all of the files:
var readFiles = ; var input = ; var hashOfFiles = {}; input;
Transform the content of the file and output it back to the stream:
var readFiles = ; var input = ; input;
Split the file into multiple files and output all of them to the stream:
var readFiles = ;var File = ; var input = ; input;
Use inside gulp
(to create a filter):
var gulp = ;var readFiles = ; gulp;