Giter VIP home page Giter VIP logo

read-vinyl-file-stream's Introduction

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'));
});

read-vinyl-file-stream's People

Contributors

catdad avatar jesstelford avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

jesstelford

read-vinyl-file-stream's Issues

handling of content is all broken in v1

This module is supposed to be compatible with a vinyl file stream. However, currently, it changes out the vinyl file read from the stream with the raw content of that vinyl file. Therefore, it cannot be piped to another vinyl transform afterwards.

See catdad/gulp-each#6.

Give more details about how the callback works

Hey, thank you for this plugin. It's probably the best one for handling streams of vinyl files easily, and seems to be the one that handles more situations.

I think the documentation about how the callback function should be called can be clearer. This is what I guessed from experimentation:

  • You can not call it with the vinyl file itself, you have to use the content
  • If you don't call the cb with the file contents, the file is omited from the stream
  • The only way to modify the file without creating a new one is to push it directly to the stream

Please correct me if any of the above assumptions is wrong. The examples are great and most of this can be deduced from them, but it will be very nice to have written confirmation of this facts.
Thanks and regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.