Open tyrak opened 5 years ago
Modern days you can use async iterables instead of duplexify
I use spawn wrapper like
const someCmd = (cmd:string) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let resolve_ = (_value?: unknown) => {
//
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let reject_ = (_e: Error) => {
//
};
const pWait = new Promise((r, rj) => {
resolve_ = r;
reject_ = rj;
});
const psql = spawn(execCmd, [...execArgs, ...commonArgs, '-c', cmd], {
stdio: ['pipe', 'pipe', process.stderr],
});
psql.on('exit', code => {
if (code != null && code > 0) {
// throw new Error('process exited with error');
console.error('EXIT CODE');
reject_(new Error('process exited with non null code'));
}
resolve_();
});
// return stream;
return async function* (source: NodeJS.ReadableStream) {
if (source != null) {
source.setEncoding('utf8');
pipeline(source, psql.stdin, err => {
if (err != null) {
reject_(err);
}
});
}
for await (const chunk of psql.stdout) {
yield chunk;
}
await pWait;
} as never;
};
Then
try {
await pipeline(
eee(),
someCmd(),
blabla()
)
} catch(e) {
<--- In case of spawn returns error code will be here
}
I am using
duplexify
to create a duplex stream out of a child process. This enables me to create complex pipelines, combining processes with other streams. So, I wrote a simple function that wrapsduplexify
andchild_process.spawn
as follows:Now I can use the stream returned by
run
withstream.pipeline
. The only issue that I have with this solution is that I would likerun
to wait for the child processes' exit status, and if it is not zero, forward an error to the duplex stream. I tried to address this by adding afinal
method as follows:but the
final
function never gets called byduplexify
. Is there a way to solve this issue withduplexify
?