1 import { app } from 'electron';
2 import Logger from 'electron-log';
3 import minimist from 'minimist';
4 import { spawn } from 'node:child_process';
5 import { existsSync } from 'node:fs';
6 import { basename, dirname, resolve } from 'node:path';
8 export const squirrelLogger = Logger.scope('squirrel');
9 export const SQUIRREL_INSTALL = '--squirrel-install';
10 export const SQUIRREL_UPDATED = '--squirrel-updated';
11 export const SQUIRREL_UNINSTALL = '--squirrel-uninstall';
12 export const SQUIRREL_OBSOLETE = '--squirrel-obsolete';
14 // Copied from `electron-squirrel-startup` and change
15 function runSquirrelShortcut(locations: string, remove: boolean) {
16 const target = basename(process.execPath);
17 const action = remove ? 'removeShortcut' : 'createShortcut';
18 const args = [`--${action}=${target}`, `--shortcut-locations=${locations}`];
19 const updateExe = resolve(dirname(process.execPath), '..', 'Update.exe');
21 squirrelLogger.debug(`Spawning "${updateExe}" with args:`, args);
22 spawn(updateExe, args, {
24 }).on('close', app.quit);
29 isAutoLaunch: boolean;
31 wantDesktopShortcut: boolean;
34 export function parseInstallArguments(lastArgs: string[]): InstallArgs {
35 const args = minimist(lastArgs);
36 squirrelLogger.log('Parsed args:', args);
39 isSilent: args.s || args.silent === 1 || args.silent === true,
40 isAutoLaunch: args['auto-launch'] !== 0,
42 wantDesktopShortcut: args['desktop-shortcut'] !== 0,
46 // Copied from `electron-squirrel-startup` and change
47 export function isSquirrelStartup() {
48 if (process.platform !== 'win32') {
52 if (process.argv.length < 2) {
56 const cmd = process.argv[1];
57 squirrelLogger.debug(`Checking for squirrel command "${cmd}"`);
59 if (cmd === SQUIRREL_INSTALL || cmd === SQUIRREL_UPDATED || cmd === SQUIRREL_UNINSTALL) {
63 if (cmd === SQUIRREL_OBSOLETE) {
71 export async function handleInstallShortcuts() {
72 const args = parseInstallArguments(process.argv);
73 squirrelLogger.log('Instal args', args);
75 // do not install desktop shortcut if not requested
76 runSquirrelShortcut(args.wantDesktopShortcut ? 'Desktop,StartMenu' : 'StartMenu', false);
79 export function handleUpdatedShortcuts() {
80 const hasDesktopShortcut = existsSync(
81 resolve(app.getPath('desktop'), basename(process.execPath).replace('.exe', '.lnk'))
84 runSquirrelShortcut(hasDesktopShortcut ? 'Desktop,StartMenu' : 'StartMenu', false);
87 export function handleUninstallShortcuts() {
88 runSquirrelShortcut('Desktop,StartMenu', true);