Skip to content

Commit

Permalink
Pass existing vinyl files through unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
svenschoenung committed May 5, 2016
1 parent 09cba4b commit fde13cb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/API.md
Expand Up @@ -4,6 +4,8 @@

Returns a [`stream.Transform`] that converts objects to [`vinyl`] files.

Existing `vinyl` files are passed through unchanged.

Each source object is stored in the `.data` property of the generated `vinyl` file. This follows the standard proposed by [`gulp-data`] and allows for interoperability with other gulp plugins.


Expand Down
5 changes: 5 additions & 0 deletions index.js
Expand Up @@ -53,6 +53,11 @@ function sanitizeContents(options) {

function vinylize(opts) {
return through(function(data, encVinylize, doneWithVinylize) {
if (File.isVinyl(data)) {
this.push(data);
doneWithVinylize();
return;
}
var options = evaluateOptions(data, opts);
if (options.path) {
options = sanitizePaths(options);
Expand Down
44 changes: 30 additions & 14 deletions test.js
Expand Up @@ -10,7 +10,7 @@ var vinylize = require('./index.js');

var streamify = require('stream-array');
var through = require('through2').obj;
var isVinyl = require('vinyl').isVinyl;
var File = require('vinyl');

function collect(files) {
return through(function(file, enc, cb) {
Expand Down Expand Up @@ -46,13 +46,13 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(2);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal('/path0/foo0/bar0.js');
expect(files[0].cwd).to.equal('/path0/');
expect(files[0].base).to.equal('/path0/foo0/');
expect(files[0].data).to.equal(obj0);
expect(isVinyl(files[1])).to.equal(true);
expect(File.isVinyl(files[1])).to.equal(true);
expect(files[1].contents.toString()).to.equal('');
expect(files[1].path).to.equal('/path1/foo1/bar1.js');
expect(files[1].cwd).to.equal('/path1/');
Expand All @@ -75,13 +75,13 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(2);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal('/path0/foo0/bar0.js');
expect(files[0].cwd).to.equal('/path0/');
expect(files[0].base).to.equal('/path0/foo0/');
expect(files[0].data).to.equal(obj0);
expect(isVinyl(files[1])).to.equal(true);
expect(File.isVinyl(files[1])).to.equal(true);
expect(files[1].contents.toString()).to.equal('');
expect(files[1].path).to.equal('/path1/foo1/bar1.js');
expect(files[1].cwd).to.equal('/path1/');
Expand All @@ -98,7 +98,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal('/path/foo/bar.js');
expect(files[0].cwd).to.equal('/path/');
Expand All @@ -116,7 +116,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal('/foo/bar.js');
expect(files[0].cwd).to.equal(process.cwd());
Expand All @@ -134,7 +134,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal('/foo/bar.js');
expect(files[0].cwd).to.equal(process.cwd());
Expand All @@ -152,7 +152,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal('/foo/bar.js');
expect(files[0].cwd).to.equal(process.cwd());
Expand All @@ -170,7 +170,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('x');
expect(files[0].path).to.equal('/foo/bar.js');
expect(files[0].cwd).to.equal(process.cwd());
Expand All @@ -188,7 +188,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal(process.cwd() + '/foo/bar.js');
expect(files[0].cwd).to.equal(process.cwd());
Expand All @@ -206,7 +206,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal(process.cwd() + '/foo/bar.js');
expect(files[0].cwd).to.equal(process.cwd());
Expand All @@ -224,7 +224,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents.toString()).to.equal('');
expect(files[0].path).to.equal('/path/foo/bar.js');
expect(files[0].cwd).to.equal('/path/');
Expand All @@ -246,7 +246,7 @@ describe('vinylize()', function() {
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(isVinyl(files[0])).to.equal(true);
expect(File.isVinyl(files[0])).to.equal(true);
expect(files[0].contents).to.have.be.instanceOf(Buffer);
expect(files[0].contents.toString()).to.equal('text');
expect(files[0].path).to.equal('/path/foo/bar.js');
Expand All @@ -256,5 +256,21 @@ describe('vinylize()', function() {
done();
});
});
it('should pass existing vinyl files through unchanged',
function(done) {
var files = [];
var file = new File({
path: '/foo/bar.js'
});
streamify([file])
.pipe(vinylize())
.pipe(collect(files))
.on('finish', function() {
expect(files).to.have.length.of(1);
expect(files[0]).to.equal(file);
done();
});
});


});

0 comments on commit fde13cb

Please sign in to comment.