Insert Command Line in typescript

Asked

Viewed 102 times

1

I have a Mongodb installation and configuration script as required by the Company.

Install_mongodb.BAT

But I would like to automate this process including in the project in Typescript. I would like to insert commands I created in . bat and include in a completed Project and Then Generate Electron Executable.

How could I insert CMD commands into Typescript ? What better way ?

  • You say, run a command on the terminal with typescript?

1 answer

1


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".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.