How to start and stop Electron application from NodeJs

Viewed 1887

I am working on a NodeJs application which helps the developers in my company with the development of our Electron-based product. It does some automatization and at the end, it starts the Electron-app automatically.

Starting the Electron app from inside NodeJs is not a problem. normally the apps are started with a bash script which looks like:

#!/bin/sh

HOME=$PWD/home-dir ./node_modules/.bin/electron myAppDir

myAppDir is the directory with my Electron-App, could be a JavaScript file as well.

Worth to mention, that ./node_modules/.bin/electron is just a symlink to ./node_modules/electron/cli.js

I did following:

const app = execFile('/the/path/to/the/bash/script', [], {
            windowsHide: true,
        },(error, stdout, stderr) => {
            if (error) {
                throw error;
            }
            warn('The app was terminated');
        });

This starts the app just fine. However if I do app.kill('SIGTERM'); it outputs the 'The app was terminated' but the app itself does not close.

I tried to execute the node_modules/.bin/electron or the ./node_modules/electron/cli.js instead:

const app = execFile('/the/path/to/node_modules/.bin/electron', ['myAppDir'], {
            windowsHide: true,
            detached: true,
            env: {
                HOME: 'path/to/home'),
            }

I can launch the Electron app but again - it does not close the running app when I do app.kill('SIGTERM');

EDIT:

My assumption is, that the electron launcher actually spawns a new subprocess, thus killing the launcher does not stops the actual launched app.

This is the content of ./node_modules/.bin/electron (or ./node_modules/electron/cli.js respectively)

#!/usr/bin/env node

var electron = require('./')

var proc = require('child_process')

var child = proc.spawn(electron, process.argv.slice(2), {stdio: 'inherit'})
child.on('close', function (code) {
  process.exit(code)
})
1 Answers
Related