How can you pass the readable stream to gulp.dest ()?

I have the following sample code that tries to bind a stream to gulp.dest ():

  var gulp = require('gulp');
  var stream = require('stream');

  var readable = new stream.Readable;
  readable.push('Hello, world!');
  readable.push(null);
  readable
  .pipe(gulp.dest('./test.txt'));

      

This code throws the following error:

path.js:146
      throw new TypeError('Arguments to path.resolve must be strings');
      ^
TypeError: Arguments to path.resolve must be strings
    at Object.win32.resolve (path.js:146:13)
    at DestroyableTransform.saveFile [as _transform] (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\lib\dest\index.js:36:26)
    at DestroyableTransform.Transform._read (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:184:10)
    at DestroyableTransform.Transform._write (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:172:12)
    at doWrite (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:237:10)
    at writeOrBuffer (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:227:5)
    at DestroyableTransform.Writable.write (C:\paylocity\expense\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:194:11)
    at Readable.ondata (_stream_readable.js:540:20)
    at Readable.emit (events.js:107:17)
    at Readable.read (_stream_readable.js:373:10)

      

I cannot, however, figure out what exactly is wrong with the code. If I replace gulp.dest () with process.stdout then it works and gulp.dest () works in the context of other calls. What is a viable way to do this?

+3


source to share


1 answer


Gulp works with vinyl streams:

var gulp   = require('gulp'),
    stream = require('stream'),
    source = require('vinyl-source-stream');

var readable = new stream.Readable;
readable.push('Hello, world!');
readable.push(null);

readable
    .pipe(source('test.txt'))
    .pipe(gulp.dest('.'));

      



A Gulp stream usually starts with some file or files as source , so you need to wrap that readable stream in a Vinyl Stream allowing Gulp and any gulp-plugin to get information from it as a filename (obviously faked), avoiding this error.

So Gulp streams are file streams, just check the source ...

+6


source







All Articles