【问题标题】:ShellJs execute CLI commandShellJs 执行 CLI 命令
【发布时间】:2021-10-08 15:25:07
【问题描述】:

我正在使用 codeceptjsshelljs

在一项测试中,我正在像这样调用go 应用程序:

const shell = require('shelljs')


shell.exec('./myGoApplication')

当应用程序启动并正常工作时,我有一个 CLI 正在通过键盘侦听来自控制台的输入,以便我可以键入文本并且我的应用程序正在获取它。

但是当我执行这个命令时,它似乎没有转移到输入并且应用程序没有调用命令。

shell.exec('my text')

有人知道如何让 shellJs 向我的 CLI 发出命令以等待控制台输入吗?

去cli:


func StartCLI(relay RelayConn) {
    go func() {
        fmt.Println("[?] To see avaliable commands write /?")
        reader := bufio.NewReader(os.Stdin)
        for {
            text, _ := reader.ReadString('\n')
            text = strings.Trim(text, "\n")

            switch true {
            case strings.HasPrefix(text, "/?"):
                fmt.Println(helpText)
    
            default:
                relay.Chat("Default Member Simulation chat message")
            }
        }
    }()
}

https://github.com/shelljs/shelljs

【问题讨论】:

    标签: javascript node.js shell codeceptjs


    【解决方案1】:

    在撰写此答案时,通过exec 启动的进程无法接受输入。详情请见issue #424

    来自ShellJS FAQ

    我们目前不支持在 exec 中运行需要交互式输入的命令。正确的解决方法是使用原生的child_process 模块:

    child_process.execFileSync(commandName, [arg1, arg2, ...], {stdio: 'inherit'});
    

    npm init为例:

    child_process.execFileSync('npm', ['init'], {stdio: 'inherit'});
    

    因此,您的代码应该是这样的:

    const child_process = require('child_process')
    
    child_process.execFileSync('./myGoApplication', {stdio: 'inherit'})
    

    stdio option设置为'inherit'意味着子进程的stdin、stdout和stderr被发送到父进程(process.stdinprocess.stdoutprocess.stderr),所以你应该是能够在您的 Node.js 程序中输入输入以将其发送到您的 Go 程序。

    查看documentation on child_process.execFileSync了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多