NodeJS。 Child_process.spawn。处理过程的输入提示 [英] NodeJS. Child_process.spawn. Handle process' input prompt

查看:480
本文介绍了NodeJS。 Child_process.spawn。处理过程的输入提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用git的网络界面。通过child_process.spawn访问git本身。有了简单的命令->响应机制,一切都很好,但是我不明白我应该如何使用命令提示符(例如, git fetch 要求输入密码)。假设发生了一些事件,但我不知道该听什么。我所看到的只是在运行node.js进程本身的命令行中的 git_user @ myserver的密码:_

I'm currently working on my web interface for git. Accessing git itself by child_process.spawn. Everything is fine while there is simple "command -> response" mechanism, but I cannot understand what should I do with command prompts (git fetch asks for password for example). Hypothetically there is some event fired, but I don't know what to listen to. All I see is "git_user@myserver's password: _" in command line where node.js process itself is running.

将这个请求重定向到我的Web应用程序中很好,但是甚至有可能吗?

It would be great to redirect this request into my web application, but is it even possible?

我尝试收听消息数据管道结束关闭可读在所有流(stdout,stdin,stderr)上运行,但没有人在密码提示时触发。

I've tried to listen on message, data, pipe, end, close, readable at all streams (stdout, stdin, stderr), but no one fires on password prompt.

这是我的工作解决方案(没有提到实验):

Here is my working solution (without mentioned experiments):

var out="";
var err="";

var proc=spawn(exe,cmd);
proc.on("exit",function(exitCode){

});
proc.stdout.on("data",function(data){
    out+=data;
});
proc.stderr.on("data",function(data){
    err+=data;
});
proc.on("close",function(code){
    if(!code)func(out);
    else return errHandler(err);
});

您能帮我进行调查吗?

更新

当前情况:在我的GIT Web界面上有一个按钮 FETCH(例如,对于简单的 git取)。当我按下它时,http请求就会生成并发送到 http.createServer(callback).listen(8080)创建的node.js服务器。 回调函数接收我的请求并创建 child_process.spawn('git',['-C','path / to / local / repo', 'fetch'])。一直以来,我在Web界面上仅看到加载屏幕,但是如果切换到运行节点脚本的命令行窗口,则会看到密码提示。现在,假设我无法将窗口切换至控制台,因为我是远程工作。

Current situation: on my GIT web interface there is a button "FETCH" (as an example, for simple "git fetch"). When I press it, http request is generated and being sent to node.js server created by http.createServer(callback).listen(8080). callback function receives my request and creates child_process.spawn('git',['-C','path/to/local/repo','fetch']). All this time I see only loading screen on my web interface, but if I switch to command line window where node script is running I will see a password prompt. Now let's pretend that I can't switch window to console, because I work remotely.

我想在Web界面上看到密码提示。例如,当提示用户输入时,child_process将在 child.stdin (或其他位置)上发出一些事件,这将非常容易实现。在这种情况下,我会向Web客户端发送字符串来吧,伙计,git想知道您的密码!在此处输入它:_______(通过 response.end(str)),并将继续等待下一个带有客户端响应的HTTP连接,其中包含所需的密码。然后只需 child.stdin.write(pass)进行git处理即可。

I want to see password prompt on my web interface. It would be very easy to achieve if, for instance, child_process would emit some event on child.stdin (or somewhere else) when prompting for user input. In that case I would send string "Come on, dude, git wants to know your password! Enter it here: _______" back to web client (by response.end(str)), and will keep on waiting for the next http connection with client response, containing desired password. Then simply child.stdin.write(pass) it to git process.

这种解决方案可能吗?或不涉及父进程命令行的东西。

Is this solution possible? Or something NOT involving command line with parent process.

UPDATE2

只是尝试过将侦听器附加到官方文档中描述的所有可能的事件:stdout和stderr(可读,数据,结束,关闭,错误),stdin(排水,结束,管道,unpipe,错误),子级(消息,退出,关闭,断开连接,

Just tried to attach listeners to all possible events described in official documentation: stdout and stderr (readable, data, end, close, error), stdin (drain, finish, pipe, unpipe, error), child (message, exit, close, disconnect, message).

在将git流传递给process.stdout,process.stderr之后,对它们进行了相同的监听。

Tried the same listeners on process.stdout, process.stderr after piping git streams to it.

密码请求无响应...

Nothing fires on password request...

推荐答案

不知道这样做是否有帮助,但我找到了唯一的方法拦截来自子进程的提示是当您生成新的子进程时将detached选项设置为true。

Don't know if this would help but I found the only way to intercept the prompts from child processes was to set the detached option to true when you spawn a new child process.

就像您一样,我找不到来自子进程的提示的任何信息在互连网上的节点中处理。有人会怀疑它应该转到stdout,然后您必须写到stdin。如果我没记错的话,您可能会发现提示已发送到stderr。

Like you I couldn't find any info on prompts from child process in node on the interwebs. One would suspect it should go to stdout and then you would have to write to stdin. If I remember correctly you may find the prompt being sent to stderr.

对我来说,其他人还没有遇到这个问题,这真让我感到惊讶。也许我们只是做错了。

Its a bit amazing to me that others haven't had this problem. Maybe we just doing it wrong.

这篇关于NodeJS。 Child_process.spawn。处理过程的输入提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆