From 4211292342d7126c17c7622c3ed8c933c828ddb5 Mon Sep 17 00:00:00 2001 From: Lalit Date: Fri, 21 Oct 2022 13:49:39 +0530 Subject: [PATCH] feat: Handling windows.squirrel events in main process --- electron/index.js | 120 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 98 insertions(+), 22 deletions(-) diff --git a/electron/index.js b/electron/index.js index 68bdde9..5160cd0 100644 --- a/electron/index.js +++ b/electron/index.js @@ -16,6 +16,71 @@ const { const isDev = require("electron-is-dev"); const { wrap: catchError } = require("./utils"); +function handleSquirrelEvent() { + if (process.argv.length === 1) { + return false; + } + + const ChildProcess = require('child_process'); + const path = require('path'); + + const appFolder = path.resolve(process.execPath, '..'); + const rootAtomFolder = path.resolve(appFolder, '..'); + const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe')); + const exeName = path.basename(process.execPath); + + const spawn = function(command, args) { + let spawnedProcess, error; + + try { + spawnedProcess = ChildProcess.spawn(command, args, {detached: true}); + } catch (error) {} + + return spawnedProcess; + }; + + const spawnUpdate = function(args) { + return spawn(updateDotExe, args); + }; + + const squirrelEvent = process.argv[1]; + switch (squirrelEvent) { + case '--squirrel-install': + case '--squirrel-updated': + // Optionally do things such as: + // - Add your .exe to the PATH + // - Write to the registry for things like file associations and + // explorer context menus + + // Install desktop and start menu shortcuts + spawnUpdate(['--createShortcut', exeName]); + + setTimeout(app.quit, 1000); + return true; + + case '--squirrel-uninstall': + // Undo anything you did in the --squirrel-install and + // --squirrel-updated handlers + + // Remove desktop and start menu shortcuts + spawnUpdate(['--removeShortcut', exeName]); + + setTimeout(app.quit, 1000); + return true; + + case '--squirrel-obsolete': + // This is called on the outgoing version of your app before + // we update to the new version - it's the opposite of + // --squirrel-updated + + app.quit(); + return true; + + default: + return false; + } +}; + const createWindow = () => { const win = new BrowserWindow({ minWidth: 1280, @@ -55,28 +120,39 @@ const addHandlers = () => { ipcMain.handle("createVault", catchError(createTable)); }; -app.whenReady().then(async () => { - try { - await connect(app); - await createTable(); - createWindow(); - - // Renderer-to-main two-way process communication - addHandlers(); - } catch (err) { - dialog.showErrorBox("Error", err.message); - app.quit(); - } -}); +const main = async () => { + if (require('electron-squirrel-startup')) return app.quit(); -app.on("window-all-closed", () => { - if (process.platform !== "darwin") { - app.quit(); + if (handleSquirrelEvent()) { + return; } -}); -app.on("activate", () => { - if (BrowserWindow.getAllWindows().length === 0) { - createWindow(); - } -}); + app.whenReady().then(async () => { + try { + await connect(app); + await createTable(); + createWindow(); + + // Renderer-to-main two-way process communication + addHandlers(); + } catch (err) { + dialog.showErrorBox("Error", err.message); + app.quit(); + } + }); + + app.on("window-all-closed", () => { + if (process.platform !== "darwin") { + app.quit(); + } + }); + + app.on("activate", () => { + if (BrowserWindow.getAllWindows().length === 0) { + createWindow(); + } + }); + +} + +main();