Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you pipe a readable stream to gulp.dest()?

Tags:

node.js

gulp

I've got the following sample code which attempts to pipe 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 produces 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 can't, however, see what exactly is wrong with the code. If I replace gulp.dest() with process.stdout then it works and the gulp.dest() works within the context of other calls. What is a viable way of doing this?

like image 820
Derek Greer Avatar asked Oct 29 '25 07:10

Derek Greer


1 Answers

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 normally begins with some file or files as source, so you need to wrap that readable stream into a Vinyl stream allowing Gulp and any gulp-plugin getting info from it like filename (obviously faked), avoiding that error of yours.

So, Gulp streams are streams of files, just check the source...

like image 89
coma Avatar answered Oct 31 '25 00:10

coma