简体   繁体   English

相当于bash管道标准输出重定向的Node.js

[英]Node.js equivalent of bash pipe stdout redirection

I am having this command I use to get the contents inside a git repository: 我正在使用此命令来获取git存储库中的内容:

git archive --remote=ssh://git@git/repository.git HEAD filename.txt | tar xvOf -

The first part of the command returns the contents of filename.txt inside the repository to the standard output. 命令的第一部分将存储库中filename.txt的内容返回到标准输出。 The second part is there in order to remove the pax_global_header that git automatically adds to the contents. 第二部分是为了删除git自动添加到内容中的pax_global_header

I want to implement this command as a spawned child_process on node.js v0.10.36 我想将此命令实现为在node.js v0.10.36上生成的v0.10.36

Here is what I've tried: 这是我尝试过的:

var git = spawn("git",  ["archive","--remote=" + repositoryUrl,"HEAD",filename]);
var tar = spawn("tar", ["xvOf", "-"]);
var out = [];
tar.stdout.pipe(git.stdout).on('data', function(data){
        var string = data.toString();
        if(string) {
            out.push(string);
        }
    });

but when I run this, the strings I get inside the data handler are like the git output never gets piped through the tar process 但是当我运行它时,我在数据处理程序中获得的字符串就像git输出从不通过tar进程传递

What am I doing wrong? 我究竟做错了什么?

I figured out the answer 10 seconds after posting this... 发布此内容后10秒钟,我就找到了答案...

What I should do was pipe the output of git to the input of tar and work with the streams outputted by tar 我应该做的是将git的输出传递到tar的输入中,并使用tar输出的流

So my code looks somewhat like this: 所以我的代码看起来像这样:

    var git = spawn("git",  ["archive","--remote=" + repositoryUrl,"HEAD",filename]);
    var tar = spawn("tar", ["xvOf", "-"]);

    var out = [];
    git.stdout.pipe(tar.stdin);

    tar.stdout.on('data', function(data){
        var string = data.toString();
        if(string) {
            out.push(string);
        }
    });
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM