From e43755b32d4adb098abe841b96faac1a2a77f69c Mon Sep 17 00:00:00 2001
From: Lee Teague <lee@perfectsensedigital.com>
Date: Fri, 6 Nov 2020 16:12:49 -0500
Subject: [PATCH] Add auto-updater

---
 package.json      |  5 +++++
 src/main/index.js | 31 +++++++++++++++++++++++++++++++
 yarn.lock         | 23 +++++++++++++++++++++++
 3 files changed, 59 insertions(+)

diff --git a/package.json b/package.json
index 63d9385..f2c41bf 100644
--- a/package.json
+++ b/package.json
@@ -24,13 +24,18 @@
     "printWidth": 120
   },
   "build": {
+    "appId": "com.psddev.shot-check",
     "asar": false,
+    "mac": {
+      "category": "public.app-category.developer-tools"
+    },
     "publish": {
       "provider": "github",
       "releaseType": "release"
     }
   },
   "dependencies": {
+    "electron-updater": "^4.3.5",
     "puppeteer": "5.3.1",
     "source-map-support": "0.5.19"
   },
diff --git a/src/main/index.js b/src/main/index.js
index 1747e33..3d43f5b 100644
--- a/src/main/index.js
+++ b/src/main/index.js
@@ -7,6 +7,7 @@ import * as os from 'os'
 import ipcListeners from './ipc/ipcListeners'
 import { isDevelopment } from '../common/appConfig'
 import { initializeExampleProjectIfNecessary } from '../common/exampleProject'
+import { autoUpdater } from 'electron-updater'
 
 // global reference to mainWindow (necessary to prevent window from being garbage collected)
 let mainWindow
@@ -78,6 +79,10 @@ app.on('ready', () => {
   initializeExampleProjectIfNecessary()
   mainWindow = createMainWindow()
   ipcListeners()
+
+  if (!isDevelopment) {
+    autoUpdater.checkForUpdates()
+  }
 })
 
 // handle file:// URLs
@@ -87,3 +92,29 @@ app.whenReady().then(() => {
     callback(pathname)
   })
 })
+
+// Auto updater
+autoUpdater.on('checking-for-update', () => {
+  console.log('Checking for Update. . .')
+})
+
+autoUpdater.on('update-available', (info) => {
+  console.log('Update Available!')
+})
+
+autoUpdater.on('update-not-available', (info) => {
+  console.log('Update Not Available!')
+})
+
+autoUpdater.on('error', (err) => {
+  console.log('Auto Updater error!', err)
+})
+
+autoUpdater.on('download-progress', (progressObj) => {
+  console.log('Download Progress:', progressObj)
+})
+
+autoUpdater.on('update-downloaded', (info) => {
+  console.log('Update Downloaded!', info)
+  autoUpdater.quitAndInstall()
+})
diff --git a/yarn.lock b/yarn.lock
index a9ca1a8..3ff1561 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2128,6 +2128,11 @@
     "@types/prop-types" "*"
     csstype "^3.0.2"
 
+"@types/semver@^7.3.1":
+  version "7.3.4"
+  resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.4.tgz#43d7168fec6fa0988bb1a513a697b29296721afb"
+  integrity sha512-+nVsLKlcUCeMzD2ufHEYuJ9a2ovstb6Dp52A5VsoKxDXgvE051XgHI/33I1EymwkRGQkwnA0LkhnUzituGs4EQ==
+
 "@types/source-list-map@*":
   version "0.1.2"
   resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
@@ -4243,6 +4248,19 @@ electron-to-chromium@^1.3.571:
   resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.576.tgz#2e70234484e03d7c7e90310d7d79fd3775379c34"
   integrity sha512-uSEI0XZ//5ic+0NdOqlxp0liCD44ck20OAGyLMSymIWTEAtHKVJi6JM18acOnRgUgX7Q65QqnI+sNncNvIy8ew==
 
+electron-updater@^4.3.5:
+  version "4.3.5"
+  resolved "https://registry.yarnpkg.com/electron-updater/-/electron-updater-4.3.5.tgz#4fb36f593a031c87ea07ee141c9f064d5deffb15"
+  integrity sha512-5jjN7ebvfj1cLI0VZMdCnJk6aC4bP+dy7ryBf21vArR0JzpRVk0OZHA2QBD+H5rm6ZSeDYHOY6+8PrMEqJ4wlQ==
+  dependencies:
+    "@types/semver" "^7.3.1"
+    builder-util-runtime "8.7.2"
+    fs-extra "^9.0.1"
+    js-yaml "^3.14.0"
+    lazy-val "^1.0.4"
+    lodash.isequal "^4.5.0"
+    semver "^7.3.2"
+
 electron-webpack-js@~2.4.1:
   version "2.4.1"
   resolved "https://registry.yarnpkg.com/electron-webpack-js/-/electron-webpack-js-2.4.1.tgz#5389cc22f34c71f6416d5ba1e043f9b0fd6130af"
@@ -6475,6 +6493,11 @@ locate-path@^5.0.0:
   dependencies:
     p-locate "^4.1.0"
 
+lodash.isequal@^4.5.0:
+  version "4.5.0"
+  resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0"
+  integrity sha1-QVxEePK8wwEgwizhDtMib30+GOA=
+
 lodash.memoize@^4.1.2:
   version "4.1.2"
   resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"