compileElectronCode = function (javascriptCode, options) {
  return new Promise((resolve, reject) => {
    function onEnd () {
      if (options.compress) data = brotliCompressSync(data);

      resolve(data);
    }

    /** @type {string} */
    const electronExecutablePath = require('electron');
    options = options || {};

    let data = Buffer.from([]);

    const electronPath = options.electronPath ? path.normalize(options.electronPath) : electronExecutablePath;
    if (!fs.existsSync(electronPath)) {
      throw new Error('Electron not found');
    }
    const bytenodePath = path.join(__dirname, 'cli.js');

    // create a subprocess in which we run Electron as our Node and V8 engine
    // running Bytenode to compile our code through stdin/stdout
    const child = spawn(electronPath, [bytenodePath, '--compile', '--no-module', '-'], {
      env: { ELECTRON_RUN_AS_NODE: '1' },
      stdio: ['pipe', 'pipe', 'pipe', 'ipc']
    });

    if (child.stdin) {
      child.stdin.write(javascriptCode);
      child.stdin.end();
    }

    if (child.stdout) {
      child.stdout.on('data', (chunk) => {
        data = Buffer.concat([data, chunk]);
      });
      child.stdout.on('error', (err) => {
        console.error(err);
      });
      child.stdout.on('end', onEnd);
    }

    if (child.stderr) {
      child.stderr.on('data', (chunk) => {
        console.error('Error: ', chunk.toString());
      });
      child.stderr.on('error', (err) => {
        console.error('Error: ', err);
      });
    }

    child.addListener('message', (message) => console.log(message));
    child.addListener('error', err => console.error(err));

    child.on('error', (err) => reject(err));
    child.on('exit', onEnd);
  });
}
This function runs the compileCode() function (above)
via a child process using Electron as Node
@param {string} javascriptCode
@param {object} [options] - optional options object
@param {string} [options.electronPath] - optional path to Electron executable, defaults to the installed node_modules/electron
@param {boolean} [options.compress]
@returns {Promise<Buffer>} - returns a Promise which resolves in the generated bytecode.