To execute a command on the terminal using Typescript, you can use the function exec
from the Nodejs native library child_process
. With her, you can do:
import { exec } from 'child_process'
exec("mkdir test", (err, stdout, stderr) => {
if (err) {
// Caso o Node não consiga efetuar o comando
return console.error(err)
}
// Resultado do programa
console.log(`stdout: ${stdout}`)
// Caso o node execute mas o comando encontre algum erro
console.log(`stderr: ${stderr}`)
})
This code will execute the command on the terminal mkdir test
creating the directory test
if all goes well. You can adapt the code to meet your needs with Electron. Note that this code should be run on main
of Electron, which has access to the Node library. From there, you can use the events, to through the client call the code in the "backend".
You say, run a command on the terminal with typescript?
– João Pedro Henrique