|
| 1 | +import path from 'path'; |
| 2 | +import { spawn } from 'child_process'; |
| 3 | +import { createLogger } from '@mongodb-js/compass-logging'; |
| 4 | +const { debug } = createLogger('SQUIRREL-STARTUP'); |
| 5 | + |
| 6 | +function runUpdateExe(args: string[]): Promise<void> { |
| 7 | + return new Promise<void>(function (resolve) { |
| 8 | + const updateExe = path.resolve( |
| 9 | + path.dirname(process.execPath), |
| 10 | + '..', |
| 11 | + 'Update.exe' |
| 12 | + ); |
| 13 | + debug('Spawning `%s` with args `%s`', updateExe, args.join(' ')); |
| 14 | + spawn(updateExe, args, { |
| 15 | + detached: true, |
| 16 | + }).on('close', () => resolve()); |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +function getShortcutName() { |
| 21 | + // The shortcut name is the app name plus the channel name if it is not |
| 22 | + // stable. ie. "MongoDB Compass Readonly Beta" |
| 23 | + |
| 24 | + const parts: string[] = [process.env.HADRON_PRODUCT_NAME]; |
| 25 | + if (process.env.HADRON_CHANNEL !== 'stable') { |
| 26 | + parts.push( |
| 27 | + process.env.HADRON_CHANNEL.charAt(0).toUpperCase() + |
| 28 | + process.env.HADRON_CHANNEL.slice(1) |
| 29 | + ); |
| 30 | + } |
| 31 | + return parts.join(' '); |
| 32 | +} |
| 33 | + |
| 34 | +/* |
| 35 | +Squirrel will spawn Compass with command line flags on first run, updates, and |
| 36 | +uninstalls. It is very important that we handle these events as early as |
| 37 | +possible, and quit immediately after handling them. Squirrel gives apps a short |
| 38 | +amount of time (~15sec) to apply these operations and quit. |
| 39 | +*/ |
| 40 | +export async function handleSquirrelWindowsStartup(): Promise<boolean> { |
| 41 | + if (process.platform !== 'win32') { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + const shortcutName = getShortcutName(); |
| 46 | + |
| 47 | + const cmd = process.argv[1]; |
| 48 | + debug('processing squirrel command `%s`', cmd); |
| 49 | + |
| 50 | + /* |
| 51 | + For more detailed info on these commands, so Electron and Squirrel Windows' |
| 52 | + documentation: |
| 53 | +
|
| 54 | + https://github.com/electron-archive/grunt-electron-installer?tab=readme-ov-file#handling-squirrel-events |
| 55 | + https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/custom-squirrel-events-non-cs.md |
| 56 | + https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/install-process.md |
| 57 | + https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/update-process.md#update-process |
| 58 | + */ |
| 59 | + switch (cmd) { |
| 60 | + case '--squirrel-install': |
| 61 | + case '--squirrel-updated': |
| 62 | + await runUpdateExe([`--createShortcut=${shortcutName}`]); |
| 63 | + debug(`${cmd} handled sucessfully`); |
| 64 | + return true; |
| 65 | + |
| 66 | + case '--squirrel-uninstall': |
| 67 | + await runUpdateExe([`--removeShortcut=${shortcutName}`]); |
| 68 | + debug(`${cmd} handled sucessfully`); |
| 69 | + return true; |
| 70 | + |
| 71 | + case '--squirrel-obsolete': |
| 72 | + debug(`${cmd} handled sucessfully`); |
| 73 | + return true; |
| 74 | + |
| 75 | + default: |
| 76 | + debug(`Unknown squirrel command: ${cmd}. Continuing on.`); |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | +} |
0 commit comments