From b90d68ba6ae6b83326f86128c6a05cdc441cdfe0 Mon Sep 17 00:00:00 2001
From: Marcin Rataj <lidel@lidel.org>
Date: Fri, 9 Oct 2020 00:45:04 +0200
Subject: [PATCH] fix(windows): autoInstallOnAppQuit

autoInstallOnAppQuit works fine on windows, but we had a bunch of code
responsible for doing it manually due to macOS not being supported
natively by autoInstallOnAppQuit.

Due to this fix from
https://github.com/ipfs-shipyard/ipfs-desktop/pull/1679 was not applied
correctly, and macOS-specific handling broke updates on windows.

This change ensures that macOS keeps using the old upgrade paths, but
Windows and AppImage leverage upstream code for applying upgrades.

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
---
 src/auto-updater/index.js                     | 27 ++++++++++++-------
 ...d-install.js => macos-quit-and-install.js} |  0
 src/daemon/index.js                           |  8 ++----
 src/index.js                                  |  2 +-
 src/tray.js                                   |  2 +-
 5 files changed, 21 insertions(+), 18 deletions(-)
 rename src/auto-updater/{quit-and-install.js => macos-quit-and-install.js} (100%)

diff --git a/src/auto-updater/index.js b/src/auto-updater/index.js
index f0a3b6f2e..729de1faf 100644
--- a/src/auto-updater/index.js
+++ b/src/auto-updater/index.js
@@ -4,13 +4,17 @@ const i18n = require('i18next')
 const logger = require('../common/logger')
 const { notify } = require('../common/notify')
 const { showDialog } = require('../dialogs')
-const quitAndInstall = require('./quit-and-install')
+const macQuitAndInstall = require('./macos-quit-and-install')
+const macOS = process.platform === 'darwin'
 
 let feedback = false
 
 function setup (ctx) {
+  // we download manually in 'update-available'
   autoUpdater.autoDownload = false
-  autoUpdater.autoInstallOnAppQuit = true
+
+  // mac requires manual upgrade, other platforms work out of the box
+  autoUpdater.autoInstallOnAppQuit = !macOS
 
   autoUpdater.on('error', err => {
     logger.error(`[updater] ${err.toString()}`)
@@ -31,7 +35,7 @@ function setup (ctx) {
   })
 
   autoUpdater.on('update-available', async ({ version, releaseNotes }) => {
-    logger.info('[updater] update available, download will start')
+    logger.info(`[updater] update to ${version} available, download will start`)
 
     try {
       await autoUpdater.downloadUpdate()
@@ -80,11 +84,14 @@ function setup (ctx) {
   })
 
   autoUpdater.on('update-downloaded', ({ version }) => {
-    logger.info('[updater] update downloaded')
+    logger.info(`[updater] update to ${version} downloaded`)
 
     const doIt = () => {
+      // Do nothing if install is handled by upstream logic
+      if (autoUpdater.autoInstallOnAppQuit) return
+      // Else, do custom install handling
       setImmediate(() => {
-        quitAndInstall(ctx)
+        if (macOS) macQuitAndInstall(ctx)
       })
     }
 
@@ -102,7 +109,7 @@ function setup (ctx) {
       message: i18n.t('updateDownloadedDialog.message', { version }),
       type: 'info',
       buttons: [
-        i18n.t('updateDownloadedDialog.action')
+        (macOS ? i18n.t('updateDownloadedDialog.action') : i18n.t('ok'))
       ]
     })
 
@@ -120,23 +127,23 @@ async function checkForUpdates () {
 
 module.exports = async function (ctx) {
   if (process.env.NODE_ENV === 'development') {
-    ctx.checkForUpdates = () => {
+    ctx.manualCheckForUpdates = () => {
       showDialog({
         title: 'Not available in development',
         message: 'Yes, you called this function successfully.',
         buttons: [i18n.t('close')]
       })
     }
-
     return
   }
 
   setup(ctx)
 
-  await checkForUpdates()
+  checkForUpdates() // background check
+
   setInterval(checkForUpdates, 43200000) // every 12 hours
 
-  ctx.checkForUpdates = () => {
+  ctx.manualCheckForUpdates = () => {
     feedback = true
     checkForUpdates()
   }
diff --git a/src/auto-updater/quit-and-install.js b/src/auto-updater/macos-quit-and-install.js
similarity index 100%
rename from src/auto-updater/quit-and-install.js
rename to src/auto-updater/macos-quit-and-install.js
diff --git a/src/daemon/index.js b/src/daemon/index.js
index 649d9b3ee..63ac0899e 100644
--- a/src/daemon/index.js
+++ b/src/daemon/index.js
@@ -109,12 +109,8 @@ module.exports = async function (ctx) {
 
   ipcMain.on('ipfsConfigChanged', restartIpfs)
 
-  app.on('before-quit', async e => {
-    if (ipfsd) {
-      e.preventDefault()
-      await stopIpfs()
-      app.quit()
-    }
+  app.on('before-quit', async () => {
+    if (ipfsd) await stopIpfs()
   })
 
   await startIpfs()
diff --git a/src/index.js b/src/index.js
index 3e7a998b9..4695501e8 100644
--- a/src/index.js
+++ b/src/index.js
@@ -69,7 +69,7 @@ async function run () {
     await setupWebUI(ctx) // ctx.webui, launchWebUI
     await setupTray(ctx) // ctx.tray
     await setupDaemon(ctx) // ctx.getIpfsd, startIpfs, stopIpfs, restartIpfs
-    await setupAutoUpdater(ctx) // ctx.checkForUpdates
+    await setupAutoUpdater(ctx) // ctx.manualCheckForUpdates
 
     await Promise.all([
       setupArgvFilesHandler(ctx),
diff --git a/src/tray.js b/src/tray.js
index e6b308247..b0ebb2ebd 100644
--- a/src/tray.js
+++ b/src/tray.js
@@ -195,7 +195,7 @@ function buildMenu (ctx) {
         { type: 'separator' },
         {
           label: i18n.t('checkForUpdates'),
-          click: () => { ctx.checkForUpdates() }
+          click: () => { ctx.manualCheckForUpdates() }
         },
         { type: 'separator' },
         {