How do you pipe a long string to /dev/stdin via child_process.spawn() in Node.js?

后端 未结 4 1458
失恋的感觉
失恋的感觉 2021-02-07 07:39

I\'m trying to execute Inkscape by passing data via stdin. Inkscape only supports this via /dev/stdin. Basically, I\'m trying to do something like this

4条回答
  •  孤街浪徒
    2021-02-07 08:04

    So, I figured out a work around. This seems like a bit of a hack, but it works just fine.

    First, I made this one line shell script:

    cat | inkscape -z -f /dev/stdin -A /dev/stdout | cat
    

    Then, I simply spawn that file and write to the stdin like this:

    cmd = spawn("shell_script");
    
    cmd.stdin.write(svg);
    cmd.stdin.end();
    cmd.stdout.pipe(pipe);
    

    I really think this should work without the shell script, but it won't (for me at least). This may be a Node.js bug.

提交回复
热议问题