Command Propt is not opening sometime on windows in electron js project

30 Views Asked by At

I am using a electron js react-boiler plate in which i have to open terminal and run a script over it my command get executed but terminal is not opening. it was working quite good on linux based systems but not working for windows based system .

Here's code snipt what i am using

ipcMain.on('run-command', (event, command) => { const terminalCommand = process.platform === 'win32' ? 'cmd' : process.platform === 'darwin' ? 'open' : 'gnome-terminal'; // Adjust for Linux

const args =
  process.platform === 'win32'
    ? ['/c', command]
    : process.platform === 'darwin'
    ? ['-a', 'Terminal', '-e', command]
    : ['--', 'bash', '-c', command]; // Adjust for Linux

const childProcess = spawn(terminalCommand, args, {
  stdio: ['pipe', 'pipe', 'pipe'], // Inherit stdio from the parent process
});

childProcess.on('error', (error) => {
  console.error(`Error spawning terminal process: ${error.message}`);
  event.reply('command-result', { error: error.message });
});

childProcess.on('exit', (code) => {
  console.log(`Terminal process exited with code ${code}`);
  event.reply('command-result', { exitCode: code });
});

});

i am using this event listener to open terminal and run script over it.

I am expecting whenever i run script on click of button the terminal needs to get open and that command need to run over the terminal and also terminal need to remain open after execution of whole script with that particular command.

Here is my code for button

const handleClick = (command: any) => { const { ipcRenderer } = window.electron;to aquire electron ipcrender to send command to IPC ipcRenderer.sendMessage('run-command', command); };

0

There are 0 best solutions below