From e5c6c6b4073d3f9acb5868f333afba2d633f6239 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Thu, 28 Feb 2019 11:47:58 +0000 Subject: [PATCH] Handle squirrel events for Windows installer --- app.js | 7 ++++ installers/setupEvents.js | 67 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 installers/setupEvents.js diff --git a/app.js b/app.js index a4df4f3b..dd0ab98c 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,10 @@ + //handle setupevents as quickly as possible + const setupEvents = require('./installers/setupEvents') + if (setupEvents.handleSquirrelEvent()) { + // squirrel event handled and app will exit in 1000ms, so don't do anything else + return; + } + const electron = require('electron') const contextMenu = require('electron-context-menu'); const path = require('path') diff --git a/installers/setupEvents.js b/installers/setupEvents.js new file mode 100644 index 00000000..a47108d1 --- /dev/null +++ b/installers/setupEvents.js @@ -0,0 +1,67 @@ +const electron = require('electron') +const app = electron.app + +module.exports = { + handleSquirrelEvent: function () { + 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; + } + } +} \ No newline at end of file