Skip to content

Commit

Permalink
New: Allow piping to multiple dest() by re-emitting each file with …
Browse files Browse the repository at this point in the history
…reset contents
  • Loading branch information
hughsk authored and phated committed Nov 28, 2017
1 parent 8740985 commit 15fd4f1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/dest/writeStream.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var streamFile = require('../src/streamFile');
var fs = require('graceful-fs');

module.exports = function(writePath, file, cb) {
Expand All @@ -8,9 +9,11 @@ module.exports = function(writePath, file, cb) {
}

var outStream = fs.createWriteStream(writePath, opt);
// TODO: can we pass the file along before the stream is unloaded?
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 @@ -275,4 +275,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 15fd4f1

Please sign in to comment.