2013-02-27 4 views
1

나는 다음과 같은 (비 작동) 코드가 있습니다node.js에서 event-stream을 사용하여 읽을 수있는 스트림을 child_process.exec 명령에 파이프하는 방법은 무엇입니까?

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 
) 

문제는 datamap() 스트림에서 작성된 체크입니다 마지막 스트림 es.child(cp.exec("cat "+data))에 자리 잡고 있습니다. 어떻게이 일을 성취 할 수 있습니까? 또한 "ls"와 "cat"은 내가 사용하고있는 실제 명령이 아니라 동적으로 생성 된 유닉스 명령을 실행하고 출력을 스트리밍하는 원칙이 동일하다는 점에 유의하십시오.

+0

수 없습니다. 당신은'child_process.spawn'을 사용해야합니다. –

답변

0

나는 이전 스트림 API를 기반으로 event-stream을 사용하지 않을 것입니다.

결함이있는 라인을 위해, 나는 이것을 테스트하지 않은 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. 
    } 
) 

사용합니다,하지만 내가 문제를 접근 할 방법이다.