Skip to content
This repository has been archived by the owner on Jan 17, 2021. It is now read-only.

Commit

Permalink
Added auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgodhani committed Mar 17, 2019
1 parent 1ed947f commit 103173a
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 133 deletions.
18 changes: 15 additions & 3 deletions package.json
@@ -1,6 +1,8 @@
{
"name": "ridereceipts",
"version": "1.8.1",
"productName": "Ride Receipts",
"version": "1.8.2",
"private": true,
"author": "Hello Efficiency Inc. <info@helloefficiency.com>",
"description": "Simple automation desktop app to download and organize your tax invoices from Uber.",
"license": "LGPL-3.0-or-later",
Expand Down Expand Up @@ -55,13 +57,23 @@
},
"mac": {
"publish": [
"github"
{
"provider": "spaces",
"name": "ridereceiptspro",
"region": "sfo2",
"path": "/ridereceipts"
}
],
"icon": "build/icons/icon.icns"
},
"win": {
"publish": [
"github"
{
"provider": "spaces",
"name": "ridereceiptspro",
"region": "sfo2",
"path": "/ridereceipts"
}
],
"certificateFile": "windows_codesign.pfx",
"certificatePassword": "hello!@#$%^",
Expand Down
16 changes: 16 additions & 0 deletions src/main/index.js
Expand Up @@ -7,6 +7,7 @@ import jetpack from 'fs-jetpack'
import fs from 'fs'
import path from 'path'
import { enforceMacOSAppLocation } from 'electron-util'
import { autoUpdateApp, checkForUpdates } from './updater.js'

import 'electron-context-menu'

Expand Down Expand Up @@ -91,6 +92,12 @@ function createWindow () {
use_version_info: false
})
},
{
label: 'Check for update',
click: function (menuItem, browserWindow, event) {
checkForUpdates(menuItem, browserWindow, event)
}
},
{
label: 'View license',
click: () => shell.openExternal('https://ridereceipts.io/license-agreement/')
Expand Down Expand Up @@ -118,6 +125,12 @@ function createWindow () {
label: 'View license',
click: () => shell.openExternal('https://ridereceipts.io/license-agreement/')
},
{
label: 'Check for update',
click: function (menuItem, browserWindow, event) {
checkForUpdates(menuItem, browserWindow, event)
}
},
{
label: `Version ${version}`,
enabled: false
Expand Down Expand Up @@ -147,6 +160,9 @@ app.on('ready', () => {
createWindow()
// Move to Application folder on MacOS
enforceMacOSAppLocation()
if (process.env.NODE_ENV === 'production') {
autoUpdateApp()
}
})

app.on('window-all-closed', () => {
Expand Down
101 changes: 101 additions & 0 deletions src/main/updater.js
@@ -0,0 +1,101 @@
import Store from 'electron-store'
import axios from 'axios'
import { API_URI } from '../renderer/config'

const { dialog } = require('electron')
const { autoUpdater } = require('electron-updater')
const log = require('electron-log')
const PRODUCT_TYPE = 'Ride Receipts Basic'

const store = new Store()

let updater
autoUpdater.autoDownload = false

autoUpdater.on('error', (error) => {
dialog.showErrorBox('Error: ', error == null ? 'Oops something went wrong.' : (error.stack || error).toString())
})

autoUpdater.on('update-available', () => {
dialog.showMessageBox({
type: 'info',
title: 'Found Updates',
message: 'Found updates, do you want update now? ✅',
buttons: ['Sure', 'No']
}, (buttonIndex) => {
if (buttonIndex === 0) {
autoUpdater.downloadUpdate()
} else {
updater.enabled = true
updater = null
}
})
})

autoUpdater.on('update-not-available', () => {
if (typeof updater !== 'undefined') {
dialog.showMessageBox({
title: 'No Updates',
message: 'Current version is up-to-date. 🎉'
})
updater.enabled = true
updater = null
}
log.info('Current version is up-to-date. 🎉')
})

autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({
title: 'Install Updates',
message: 'Updates downloaded, application will be quit for update...'
}, () => {
setImmediate(() => autoUpdater.quitAndInstall())
})
})

export const autoUpdateApp = () => {
const licensekey = store.get('license_key')
axios.post(`${API_URI}/license/verify`, { license_key: licensekey, product_type: PRODUCT_TYPE }).then((res) => {
const expired = res.data.data.expired
if (!expired) {
autoUpdater.checkForUpdates()
} else {
dialog.showMessageBox({
title: 'No Updates',
message: 'Your license key is expired. Please renew license key by repurchasing it from https://ridereceipts.io. ❌'
})
}
}).catch((err) => {
if (err) {
log.error(err)
log.info('No license found in database')
}
})
}

// export this to MenuItem click callback
export const checkForUpdates = (menuItem, focusedWindow, event) => {
updater = menuItem
updater.enabled = false
const licensekey = store.get('license_key')
axios.post(`${API_URI}/license/verify`, { license_key: licensekey, product_type: PRODUCT_TYPE }).then((res) => {
const expired = res.data.data.expired
if (!expired) {
autoUpdater.checkForUpdates()
} else {
dialog.showMessageBox({
title: 'No Updates',
message: 'Your license key is expired. Please renew license key by repurchasing it from https://ridereceipts.io. ❌'
})
}
}).catch((err) => {
if (err) {
log.error(err)
dialog.showMessageBox({
title: 'No Updates',
message: 'License key not found in our database. ☹'
})
updater.enabled = true
}
})
}

0 comments on commit 103173a

Please sign in to comment.