简体   繁体   English

外壳程序命令child_process.spawn(command,[args],[options])node.js

[英]shell command to child_process.spawn(command, [args], [options]) node.js

Basically, I just want to run rsync command on node app. 基本上,我只想在节点应用程序上运行rsync命令。

The raw rsync code is the below: 原始的rsync代码如下:

rsync -r -t -p -o -g -v --progress --delete -l -H /Users/ken/Library/Application Support/Sublime Text 3/Packages /Users/ken/Google Drive/__config-GD/ST3

Firstly, I tried 首先,我尝试

child_process.exec(command, [options], callback)

but, since the rsync output is long, the buffer exceeded. 但是,由于rsync输出很长,因此超出了缓冲区。

So, I tried 所以,我尝试

child_process.spawn(command, [args], [options])

instead, and stream.pipe out. 而是stream.pipe出来。

var spawn = require('child_process')
        .spawn;
var ps = spawn(command1, args1)
        .stdout
        .pipe(process.stdout);

Although this works as expected, it is way too hard to split every original rsync command to the spawn format. 尽管这可以按预期工作,但是将每个原始rsync命令拆分为派生格式太难了。 (I copied and paste the command from gRsync tool) (我从gRsync工具复制并粘贴了命令)

Is there smart way to do this? 有什么聪明的方法吗?

Thanks. 谢谢。

Ok, here is the hack; 好的,这是黑客。

Escape white space of the MacDirectory in the terminal shell and javascript/node is very tricky, so I placed _@_ instead of \\ ('\\'+whiteSpace). 在终端外壳和javascript / node中,MacDirectory的转义空格非常棘手,因此我放置了_@_而不是\\ ('\\'+ whiteSpace)。

(function()
{
    'use strict';
    //,,,,,,,,,,,,,,

    var parseShCmd = function(cmd)
    {
        var cmdA = cmd.split(/ /);
        console.log(cmdA);
        var cmd1 = cmdA[0];
        var args1 = [];
        cmdA.map(function(s, i)
        {
            if (i === 0)
            {
                console.log('-------------------'); //do nothing
            }
            else
            {
                args1[i - 1] = cmdA[i].replace(/_@_/g, ' ');
            }
        });
        return [cmd1, args1];
    };

    var shCmd = 'rsync' +
        ' -r -t -p -o -g -v --progress --delete -l -H ' +
        '/Users/ken/Library/Application_@_Support/Sublime_@_Text_@_3/Packages ' +
        '/Users/ken/Google_@_Drive/__config-GD/ST3';
    //var shCmd = 'ls -la';
    var cmd1 = parseShCmd(shCmd);
    console.log(cmd1[0]);
    console.log(cmd1[1]);
    console.log('==================');
    var spawn = require('child_process')
        .spawn;
    var ps = spawn(cmd1[0], cmd1[1])
        .stdout
        .pipe(process.stdout);

    //,,,,,,,,,,,,,,,,
}());

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

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