Skip to content

Instantly share code, notes, and snippets.

@fearphage
Created June 15, 2018 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fearphage/e99a318a6d93146665c1b825f7a6b90b to your computer and use it in GitHub Desktop.
Save fearphage/e99a318a6d93146665c1b825f7a6b90b to your computer and use it in GitHub Desktop.
Sends content before and after another stream

Below you will find the contents of some-file.txt:

just a file

That's all folks!

const fs = require('fs');
const wrapStream = require('./wrap-stream');
fs.createReadStream('some-file.txt') // file contents: 'just a file'
.pipe(wrapStream('## Below you will find the contents of some-file.txt:\n\n```\n', '\n```\n\nThat\'s all folks!')
.pipe('output.md')
;
function wrapStream(prefix, suffix) {
let once = false;
return new Transform({
objectMode: true,
transform(chunk, _, callback) {
if (!once) {
once = true;
callback(null, `${prefix}${chunk}`);
}
else {
callback(null, chunk);
}
},
flush(callback) {
callback(null, suffix);
}
});
}
module.exports = wrapStream;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment