javascriptnode.jsshellcodeceptjs

ShellJs execute CLI command


I'm using codeceptjs with shelljs.

In one of tests I'm invoking go application like this :

const shell = require('shelljs')


shell.exec('./myGoApplication')

When application is started and correctly working I have a CLI that is listening for input from console via keyboard so I can type text and my application is getting it.

But when I execute this command seems its not transferring to the input and application is not invoking commands.

shell.exec('my text')

Does someone know how to make shellJs make command to my CLI waiting for console input?

go 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


Solution

  • As of writing this answer, it is not possible for processes launched via exec to accept input. See issue #424 for details.

    From the ShellJS FAQ:

    We don't currently support running commands in exec which require interactive input. The correct workaround is to use the native child_process module:

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

    Using npm init as an example:

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

    Your code should therefore be something like this:

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

    Setting the stdio option to 'inherit' means that stdin, stdout, and stderr of the child process is sent to the parent processes' (process.stdin, process.stdout, and process.stderr), so you should be able to type in input to your Node.js program to send it to your Go program.

    See the documentation on child_process.execFileSync for more deatils.