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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.