Check if git is installed using Node

8

I'm starting in a desktop application with the use of Electron however I need to check if there is Git installed in the system because it must necessarily use git

As the idea is to distribute in Windows, Linux and Mac and the verification should occur at every application startup how can I check this on Linux and Mac?

What I have by the hour:

const {
    spawn,
    spawnSync,
    exec,
    execSync,
    execFileSync,
} = require('child_process')

switch(process.platform){
    case'win32':
        let git = spawn('cmd.exe', ['/c', 'git --version'])
        git.stdout.on('data', (data) => {
            // logic...
        });
        break;
    case'linux':
        //
        break;
    case'darwin':
        //
        break;
    default:
        // unsupported
    break;
}
    
asked by anonymous 08.08.2017 / 16:15

2 answers

8

I have not yet had the opportunity to use Node and everything, but I think you can call git directly, you do not need to call the shell executable to do this.

let git = spawn('git', ['--version'])

In addition, for what you want, according to the link that I'm going to put in the source, the most used execFile is for this:

const child = execFile('git', ['--version'], (error, stdout, stderr) => {
  if (error) {
      console.error('stderr', stderr);
      throw error;
  }
  console.log('stdout', stdout);
});
  

When?

     

ExecFile is used when we just need to run an application and   get the output. For example, we can use execFile to perform a   image processing application such as ImageMagick for   convert a PNG image to JPG format and we only   we worry whether she is successful or not. ExecFile should not be   used when the external application produces a large amount of   data and we need to consume this data in real time.

Source

link

    
11.08.2017 / 15:16
0

The simplest solution I thought was this:

const platform = process.platform
const { spawn } = require('child_process')
const where = platform === 'win32' ? 'where' : 'which'
const out = spawn(where, ['ls'])

out.on('close', (code) => {
  console.log('child process exited with code ${code}') // 0 se o comando existe, 1 se não existe
});
    
16.08.2017 / 06:31