Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I interact with this <File> object in a node.js stream?

Tags:

I'm using gulp to build a stream of glob-matched files and move them all, in their nested structure, to a new location. To do this, I first wanted to build a simple 'through' stream to see what I get passed if I pipe to it from gulp.src().

Here is my test gulpfile.js:

var through = require("through"); var fs = require("fs");  function write(file) {   console.log(file);   console.log(file.toString()); }  gulp.task("move", function () {    return gulp.src("./**")     .pipe(through(write));  }); 

If I run the gulp 'move' task on the command line, I get output like the following:

<File "some/path"> [object Object] <File "some/path/file.js" <Buffer 2f 2a 0a 0a 4f 72 67 69 6e 61 6c 20 53 74 79 6c 65 20 66 72 6f 6d 20 65 74 68 61 6e 73 63 68 6f 6f 6e 6f 76 65 72 2e 63 6f 6d 2f 73 6f 6c 61 72 69 7a 65 ...>> [object Object] 

What are those objects? How can I interact with them?

like image 623
rhodesjason Avatar asked Mar 09 '14 19:03

rhodesjason


People also ask

How do I read a node JS stream file?

Next, you will create a function below the switch statement called read() with a single parameter: the file path for the file you want to read. This function will create a readable stream from that file and listen for the data event on that stream. The code also checks for errors by listening for the error event.

Which object is stream in node JS?

Streams are objects that allows developers to read/write data to and from a source in a continuous manner. There are four main types of streams in Node. js; readable, writable, duplex and transform. Each stream is an eventEmitter instance that emits different events at several intervals.

How do you make an object readable stream?

To implement a readable stream, we require the Readable interface, and construct an object from it, and implement a read() method in the stream's configuration parameter: const { Readable } = require('stream'); const inStream = new Readable({ read() {} }); There is a simple way to implement readable streams.

Which object is a stream?

The object stream classes are ObjectInputStream and ObjectOutputStream . These classes implement ObjectInput and ObjectOutput , which are subinterfaces of DataInput and DataOutput . That means that all the primitive data I/O methods covered in Data Streams are also implemented in object streams.


2 Answers

Those are vinyl objects. They are the core data type passed through gulp streams. The contain information about the file (such as path info and contents as a buffer or stream). You can see the data better using gulp-debug.

If you want to move a bunch of files, while preserving their relative path, you can do one of the following, no need to dig into the code yourself:

gulp.src('/a/single/src/path/**/*.foo').pipe(gulp.dest('/a/single/dest/path')); 

Or, if you have a bunch of different globs:

gulp.src(['/a/src/path/foo/**/*.foo', '/a/src/path/bar/**/*.bar'], {base: '/a/src/path/'})     .pipe(gulp.dest('/a/dest/path/')); 

Mostly you'll be using gulp plugins to manipulate the files, then passing the result to gulp.dest(), rather than manipulating them yourself.

If you need to manipulate the files, there's a few plugins that can help:

  • gulp-tap allows you to peak into the stream, and optionally modify the file or buffer.
  • vinyl-map lets you easily modify the contents of files
  • gulp-filter can help you filter the stream if globbing doesn't work.
like image 152
OverZealous Avatar answered Sep 21 '22 15:09

OverZealous


You can view the file properties using this js:

var propValue; for(var propName in file) {     propValue = file[propName];     console.log('name:' + propName, ', value:<<<',propValue,'>>>'); }  Sample Output     name:history ,     value:"C:\Temp\test.txt"     name:cwd ,         value:"C:\Temp"     name:base ,        value:"C:\Temp"     name:_contents ,   value: full file contents     name:isBuffer ,    value:"function () {     name:isStream ,    value:"function () {     name:isNull ,      value:"function () {     name:isDirectory , value:"function () {     name:clone ,       value:"function (opt) {     name:pipe ,        value:"function (stream, opt) {     name:inspect ,     value:"function () {     name:stat , value:<<< { dev: 0,       mode: 33206,       nlink: 1,       uid: 0,       gid: 0,       rdev: 0,       ino: 0,       size: 874,       atime: Sat Sep 19 2015 14:34:51 GMT+1000 (AUS Eastern Standard Time),       mtime: Sat Sep 19 2015 14:34:51 GMT+1000 (AUS Eastern Standard Time),       ctime: Sat Sep 12 2015 14:59:40 GMT+1000 (AUS Eastern Standard Time) } >>>  Usage: console.log('file name:', file.relative); console.log('file current working directory:', file.cwd); console.log('file isDirectory:', file.isDirectory()); 
like image 41
user1491819 Avatar answered Sep 17 '22 15:09

user1491819