mongodb-js / electron-squirrel-startup

Default Squirrel.Windows event handler for your Electron apps.
Apache License 2.0
216 stars 41 forks source link

ESM example/usage #49

Open rtritto opened 3 weeks ago

rtritto commented 3 weeks ago

With ESM, the electron-squirrel-startup dependency can't be found if it's in dependencies (it works if I move it into devDependencies):

Note: an example for ESM use case should be added in README.md.

Reproduction

template-electron-vite

uncaught commented 1 week ago

I've simply copied the script and converted it to ESM locally:

import path from 'path';
import {spawn} from 'child_process';
import _debug from 'debug';
import {app} from 'electron';

//Copied from https://github.com/mongodb-js/electron-squirrel-startup/blob/master/index.js due to missing ESM support

const debug = _debug('electron-squirrel-startup');

function run(args: string[], done: () => void) {
  const updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe');
  debug('Spawning `%s` with args `%s`', updateExe, args);
  spawn(updateExe, args, {
    detached: true,
  }).on('close', done);
}

function check(): boolean {
  if (process.platform === 'win32') {
    const cmd = process.argv[1];
    debug('processing squirrel command `%s`', cmd);
    const target = path.basename(process.execPath);

    if (cmd === '--squirrel-install' || cmd === '--squirrel-updated') {
      run(['--createShortcut=' + target + ''], app.quit);
      return true;
    }
    if (cmd === '--squirrel-uninstall') {
      run(['--removeShortcut=' + target + ''], app.quit);
      return true;
    }
    if (cmd === '--squirrel-obsolete') {
      app.quit();
      return true;
    }
  }
  return false;
}

export default check();