1

I have the following (non-functioning) code:

var es = require('event-stream');
var cp = require('child_process');

es.pipeline(
    es.child(cp.exec("ls")),
    es.split(/[\t\s]+/),
    es.map(function(data,cb){
        if ( /\.txt$/.test(data) ) cb(null, data);
        else cb();
    }),
    es.child(cp.exec("cat "+data)) // this doesn't work
)

The problem lies in the last stream es.child(cp.exec("cat "+data)) where data is the chunk written from the map() stream. how would one go about achieving this? Also please note that "ls" and "cat" are not the actual commands I am using but the principle of executing a dynamically-generated unix command and streaming the output is the same.

AndyPerlitch
  • 4,539
  • 5
  • 28
  • 43

1 Answers1

0

I wouldn't use event-stream, it based on a older stream API.

For the faulty line, I would use through2

var thr = require('through2').obj
var es = require('event-stream');
var cp = require('child_process');

function finalStream (cmd) {
  return thr(function(data, enc, next){
    var push = this.push

    // note I'm not handling any error from the child_process here
    cp.exec(cmd +' '+ data).stdout.pipe(thr(function(chunk, enc, next){
      push(chunk)
      next()
    }))
    .on('close', function(errorCode){
      if (errorCode) throw new Error('ops')
      next()
    })

  })
}

es.pipeline(
    es.child(cp.exec("ls")),
    es.split(/[\t\s]+/),
    es.map(function(data,cb){
        if ( /\.txt$/.test(data) ) cb(null, data);
        else cb();
    }),
    finalStream('cat') 
    thr(function(chunk, enc, next){
      // do stuff with the output of cat.
    }
)

I haven't test this, but this is how I would approach the problem.

markuz-gj
  • 219
  • 1
  • 8