Bundle a CLI app inside the Electron app


#1

I’m building an app that has a GUI interface (Electron) and a CLI interface. The CLI app is built in Node and I’d like to bundle it inside the Electron app. This way, the user will not be required to install Node on his machine because the CLI app will use the Node binary from the Electron app.

Is it possible to achieve this? Can you quickly describe how should I approach this problem?


#2

A simpler approach : your Electron app binary can be used as a CLI binary (/Application/Your App.app/Contents/MacOS/your_app).

In your app.js (browser process), you can simply parse process.argv to check how it was invoked and take action.

The great thing with this approach is : if your app is already running and you call the binary from the command line, you will be in the running instance if you do something like this:

const handleCommandLine = (argv, workingDir) => {
  if (process.argv.length === 1) return
  // ... do something with command line arguments
}
// parse the command line right now
handleCommandLine(process.argv, process.cwd())

// the following invoked on the first process when another a new one is launched
const shouldQuit = app.makeSingleInstance((argv, workingDirectory) => {
  handleCommandLine(argv, workingDirectory)
})
if (shouldQuit) {
  app.quit()
}