|
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import os from 'os'; |
| 3 | +import path from 'path'; |
| 4 | +import { spawn } from 'child_process'; |
| 5 | +import { createLogger } from '@mongodb-js/compass-logging'; |
| 6 | +const { debug } = createLogger('SQUIRREL-STARTUP'); |
| 7 | + |
| 8 | +function runUpdateExe(args: string[]): Promise<void> { |
| 9 | + return new Promise<void>(function (resolve) { |
| 10 | + const updateExe = path.resolve( |
| 11 | + path.dirname(process.execPath), |
| 12 | + '..', |
| 13 | + 'Update.exe' |
| 14 | + ); |
| 15 | + debug('Spawning `%s` with args `%s`', updateExe, args.join(' ')); |
| 16 | + spawn(updateExe, args, { |
| 17 | + detached: true, |
| 18 | + }).on('close', () => resolve()); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +function getShortcutName() { |
| 23 | + return process.env.HADRON_PRODUCT_NAME ?? 'MongoDB Compass'; |
| 24 | +} |
| 25 | + |
| 26 | +async function renameShortcuts(from: string, to: string) { |
| 27 | + const folders: string[] = []; |
| 28 | + |
| 29 | + if (process.env.APPDATA) { |
| 30 | + // start menu shortcut |
| 31 | + // NOTE: This includes the non-ideal "MongoDB Inc" folder name as opposed to |
| 32 | + // "MongoDB". |
| 33 | + folders.push( |
| 34 | + path.join( |
| 35 | + process.env.APPDATA, |
| 36 | + 'Microsoft', |
| 37 | + 'Windows', |
| 38 | + 'Start Menu', |
| 39 | + 'Programs', |
| 40 | + 'MongoDB Inc' |
| 41 | + ) |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + // desktop shortcut |
| 46 | + folders.push(path.join(os.homedir(), 'Desktop')); |
| 47 | + |
| 48 | + for (const folder of folders) { |
| 49 | + const source = path.join(folder, from); |
| 50 | + const destination = path.join(folder, to); |
| 51 | + try { |
| 52 | + // only rename the shortcut if it exists at the standard, expected location |
| 53 | + if (await fs.stat(source)) { |
| 54 | + debug('renaming shortcut from %s to %s', source, destination); |
| 55 | + await fs.rename(source, destination); |
| 56 | + } else { |
| 57 | + debug('shortcut %s does not exist, skipping rename', source); |
| 58 | + } |
| 59 | + } catch (err: unknown) { |
| 60 | + debug( |
| 61 | + 'error renaming shortcut from %s to %s: %s', |
| 62 | + source, |
| 63 | + destination, |
| 64 | + err |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/* |
| 71 | +Squirrel will spawn Compass with command line flags on first run, updates, and |
| 72 | +uninstalls. It is very important that we handle these events as early as |
| 73 | +possible, and quit immediately after handling them. Squirrel gives apps a short |
| 74 | +amount of time (~15sec) to apply these operations and quit. |
| 75 | +*/ |
| 76 | +export async function handleSquirrelWindowsStartup(): Promise<boolean> { |
| 77 | + if (process.platform !== 'win32') { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + // This has to be an executable that was packaged up with the app. There's no |
| 82 | + // way to control what the shortcut name will be - it is always the same as |
| 83 | + // the executable. |
| 84 | + const exeName = path.basename(process.execPath); |
| 85 | + |
| 86 | + // We want the desktop and start menu shortcuts to be named with spaces in |
| 87 | + // them so that when the user searches for "compass" in the start menu it will |
| 88 | + // find it. |
| 89 | + const correctShortcutName = getShortcutName() + '.lnk'; |
| 90 | + const wrongShortcutName = getShortcutName().replace(/ /g, '') + '.lnk'; |
| 91 | + |
| 92 | + const cmd = process.argv[1]; |
| 93 | + debug('processing squirrel command `%s`', cmd); |
| 94 | + |
| 95 | + /* |
| 96 | + For more detailed info on these commands, so Electron, electron-winstaller and Squirrel Windows' |
| 97 | + documentation: |
| 98 | +
|
| 99 | + https://github.com/electron/windows-installer?tab=readme-ov-file#handling-squirrel-events |
| 100 | + https://github.com/electron-archive/grunt-electron-installer?tab=readme-ov-file#handling-squirrel-events |
| 101 | + https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/custom-squirrel-events-non-cs.md |
| 102 | + https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/install-process.md |
| 103 | + https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/update-process.md#update-process |
| 104 | + */ |
| 105 | + switch (cmd) { |
| 106 | + case '--squirrel-install': |
| 107 | + case '--squirrel-updated': |
| 108 | + await runUpdateExe([`--createShortcut=${exeName}`]); |
| 109 | + // Rename the shortcut that was just created to be what we want it to be. |
| 110 | + await renameShortcuts(wrongShortcutName, correctShortcutName); |
| 111 | + debug(`${cmd} handled sucessfully`); |
| 112 | + return true; |
| 113 | + |
| 114 | + case '--squirrel-uninstall': |
| 115 | + // Rename the shortcut back to the original name so it can be removed as |
| 116 | + // usual. |
| 117 | + await renameShortcuts(correctShortcutName, wrongShortcutName); |
| 118 | + await runUpdateExe([`--removeShortcut=${exeName}`]); |
| 119 | + debug(`${cmd} handled sucessfully`); |
| 120 | + return true; |
| 121 | + |
| 122 | + case '--squirrel-obsolete': |
| 123 | + debug(`${cmd} handled sucessfully`); |
| 124 | + return true; |
| 125 | + |
| 126 | + default: |
| 127 | + debug(`Unknown squirrel command: ${cmd}. Continuing on.`); |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | +} |
0 commit comments