Skip to content

Commit

Permalink
Merge pull request #10 from hughsk/multiple-dests
Browse files Browse the repository at this point in the history
Allow piping streaming vinyls through multiple .dest()'s
  • Loading branch information
yocontra committed Feb 26, 2014
2 parents f2f11f3 + 12cce4f commit 3da9acc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/dest/writeStream.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
var streamFile = require('../src/streamFile');
var fs = require('graceful-fs');

module.exports = function(writePath, file, cb) {
var outStream = fs.createWriteStream(writePath);

file.contents.once('error', cb);
outStream.once('finish', cb);
file.contents.pipe(outStream).once('finish', function() {
streamFile(file, cb);
});

file.contents.pipe(outStream);
return outStream;
};
};
38 changes: 38 additions & 0 deletions test/dest.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,42 @@ describe('dest stream', function() {
stream.end();
});

it('should allow piping multiple dests in streaming mode', function(done) {
var inputPath1 = path.join(__dirname, "./out-fixtures/multiple-first");
var inputPath2 = path.join(__dirname, "./out-fixtures/multiple-second");
var inputBase = path.join(__dirname, "./out-fixtures/");
var srcPath = path.join(__dirname, "./fixtures/test.coffee");
var stream1 = vfs.dest('./out-fixtures/', {cwd: __dirname});
var stream2 = vfs.dest('./out-fixtures/', {cwd: __dirname});
var content = fs.readFileSync(srcPath);
var rename = through.obj(function(file, _, next) {
file.path = inputPath2;
this.push(file);
next();
});

stream1.on('data', function(file) {
file.path.should.equal(inputPath1);
})

stream1.pipe(rename).pipe(stream2);
stream2.on('data', function(file) {
file.path.should.equal(inputPath2);
}).once('end', function() {
fs.readFileSync(inputPath1, 'utf8').should.equal(content.toString());
fs.readFileSync(inputPath2, 'utf8').should.equal(content.toString());
done();
});

var file = new File({
base: inputBase,
path: inputPath1,
cwd: __dirname,
contents: content
})

stream1.write(file);
stream1.end();
})

});

0 comments on commit 3da9acc

Please sign in to comment.