Is there a way to add a file into the gulp stream with through2's flush function? I tried the following, but I got an error:
gulp.task('chapters', function () {
return gulp.src('chapters/*.md')
.pipe(hello())
.pipe(gulp.dest('chapters/'))
})
function hello () {
return through.obj(function (file, enc, cb) {
this.push(file)
cb()
}, (flush) => {
var json = new gutil.File({
base: __dirname,
cwd: __dirname,
path: path.join(__dirname, 'chapters.json')
})
json.contents = new Buffer(JSON.stringify({test: 'test'}))
this.push(json)
flush()
})
}
The error is
/Users/zellwk/Projects/private-repos/rt/gulpfile.js:59
this.push(json)
^
TypeError: this.push is not a function
at DestroyableTransform._flush (/Users/zellwk/Projects/private-repos/rt/gulpfile.js:59:10)
at DestroyableTransform.<anonymous> (/Users/zellwk/Projects/private-repos/rt/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:115:49)
at DestroyableTransform.g (events.js:273:16)
at emitNone (events.js:80:13)
at DestroyableTransform.emit (events.js:179:7)
at prefinish (/Users/zellwk/Projects/private-repos/rt/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:465:12)
at finishMaybe (/Users/zellwk/Projects/private-repos/rt/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:473:7)
at endWritable (/Users/zellwk/Projects/private-repos/rt/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:485:3)
at DestroyableTransform.Writable.end (/Users/zellwk/Projects/private-repos/rt/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:455:41)
at DestroyableTransform.onend (/Users/zellwk/Projects/private-repos/rt/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:523:10)
Apparently, this.push is undefined in the flush function. However, through2's documentation clearly states that this is possible. Any workarounds?
You're using an arrow function which changes what this means
Although it seems this has already been answered, support questions should be directed to StackOverflow.
oh! Okay thanks
Most helpful comment
You're using an arrow function which changes what
thismeans