Open terminal and launch commands

时间:2019-03-19 14:46:18

标签: node.js electron

In my electron/reactjs app, i'm trying to open a terminal and launch somes commands.

My code looks like this :

const terminal = 'x-terminal-emulator';
const { spawn } = require('child_process');
spawn(terminal);

My terminal opens but i don't know how to launch commands in this terminal like 'cd /my/custom/path && ls'

Can someone help me please ? :)

1 个答案:

答案 0 :(得分:0)

Node.js child_process.spawn命令具有一个option,用于指定您要使用的外壳。

因此,我将使用相反的逻辑,并在特定的shell中直接启动命令(例如 bash ):

const { spawn } = require('child_process');
const terminal = '/bin/bash';

let cmd = 'echo $SHELL';

spawn(cmd, { shell: terminal })
  .stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);  //-> stdout: /bin/bash
});