To execute a command on the terminal using TypeScript, you can use the exec
function of the native library of% Node child_process
. With it, 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. It is worth mentioning that this code must be run in main
of Electron, which is the one that has access to the Node library. From there, you can use the events, for through the client to call the code in the "backend".