npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

read-vinyl-file-stream

v2.0.3

Published

process a vinyl file stream with minimal code

Downloads

74,792

Readme

read vinyl file stream

Build Test Coverage Code Climate Downloads Version Dependency Status

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 = require('read-vinyl-file-stream');

var input = getVinylStream();

var hashOfFiles = {};

input.pipe(readFiles(function (content, file, stream, cb) {
    hashOfFiles[file.path] = content;

    cb();
}));

Transform the content of the file and output it back to the stream:

var readFiles = require('read-vinyl-file-stream');

var input = getVinylStream();

input.pipe(readFiles(function (content, file, stream, cb) {
    var newContent = doWorkToTheContent(content);

    cb(null, newContent);
}));

Split the file into multiple files and output all of them to the stream:

var readFiles = require('read-vinyl-file-stream');
var File = require('vinyl');

var input = getVinylStream();

input.pipe(readFiles(function (content, file, stream, cb) {
    var lines = content.split('\n');

    lines.forEach(function (line, idx) {
        stream.push(new File({
            contents: new Buffer(line),
            path: file.path + 'line' + idx
        }));
    });

    cb();
}));

Use inside gulp (to create a filter):

var gulp = require('gulp');
var readFiles = require('read-vinyl-file-stream');

gulp.task('mytask', function() {
    return gulp.src('*.ext')
        .pipe(readFiles(function (content, file, stream, cb) {
            if (/^n/.test(content)) {
                return cb(null, content);
            }

            cb();
        }))
        .pipe(gulp.dest('filesThatStartWithN'));
});