Skip to content

Commit

Permalink
Fix updater
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Aug 27, 2020
1 parent 46ab7df commit e0cd267
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 32 deletions.
48 changes: 30 additions & 18 deletions src/main/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,37 @@ export const appResourcesPath = (): string => {
return resourcesPath
}

export const platform = (): NodeJS.Platform => {
let platform = os.platform()
if (process.env.KEYS_PLATFORM) {
platform = process.env.KEYS_PLATFORM as NodeJS.Platform
}
return platform
}

export const appSupportPath = (): string => {
const appName: string = getAppName()
const appName = getAppName()
let supportDir
if (os.platform() == 'linux') {
if (process.env.XDG_DATA_HOME) {
supportDir = process.env.XDG_DATA_HOME
} else {
const homeDir = os.homedir()
supportDir = path.join(homeDir, '.local', 'share')
}
} else if (os.platform() == 'win32') {
if (process.env.LOCALAPPDATA) {
supportDir = process.env.LOCALAPPDATA
} else {
const homeDir = os.homedir()
supportDir = path.join(homeDir, 'AppData', 'Roaming')
}
} else {
supportDir = app.getPath('appData')
switch (platform()) {
case 'linux':
if (process.env.XDG_DATA_HOME) {
supportDir = process.env.XDG_DATA_HOME
} else {
const homeDir = os.homedir()
supportDir = path.join(homeDir, '.local', 'share')
}
break
case 'win32':
if (process.env.LOCALAPPDATA) {
supportDir = process.env.LOCALAPPDATA
} else {
const homeDir = os.homedir()
supportDir = path.join(homeDir, 'AppData', 'Roaming')
}
break
default:
supportDir = app.getPath('appData')
break
}

const p = path.join(supportDir, appName)
Expand Down Expand Up @@ -60,7 +72,7 @@ export const appPath = (): string => {
// Path to an executable
export const binPath = (name: string): string => {
const resourcesPath = appResourcesPath()
if (os.platform() == 'win32') {
if (platform() == 'win32') {
name = name + '.exe'
}
return path.join(resourcesPath, 'bin', name)
Expand Down
4 changes: 2 additions & 2 deletions src/main/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {app} from 'electron'
import {appPath, appSupportPath} from './paths'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import {platform} from './paths'
import {keysStop} from './service'
import ElectronStore from 'electron-store'

Expand Down Expand Up @@ -77,7 +77,7 @@ export const update = (version: string, apply: boolean): Promise<UpdateResult> =
console.log('Apply:', applyPath)
relaunch = true

if (os.platform() == 'win32') {
if (platform() == 'win32') {
// Copy updater to support path, so we can update over the installed version.
const updaterDest = path.join(appSupportPath(), 'updater.exe')
if (fs.existsSync(updaterDest)) {
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as os from 'os'

export const platform = (): NodeJS.Platform => {
let platform = os.platform()
if (process.env.KEYS_PLATFORM) {
platform = process.env.KEYS_PLATFORM as NodeJS.Platform
}
return platform
}
1 change: 1 addition & 0 deletions src/renderer/views/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export default (_: {}) => {
<Router hook={useHashLocation}>
<Root />
<Errors />
<UpdateAlert />
</Router>
</ThemeProvider>
)
Expand Down
51 changes: 39 additions & 12 deletions src/renderer/views/update/alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {Box, Snackbar, Typography} from '@material-ui/core'
import {Link} from '../../components'

import Alert from '@material-ui/lab/Alert'

import {platform} from '../../env'
import {store} from '../../store'
import {ipcRenderer} from 'electron'
import {ipcRenderer, shell} from 'electron'

export default (props: {}) => {
const setUpdating = () => {
Expand All @@ -34,6 +34,7 @@ class UpdateAlert extends React.Component<Props, State> {
}

componentDidMount() {
console.log('UpdateAlert mount')
ipcRenderer.on('update-needed', (event, update) => {
console.log('Update:', update)
if (update.needUpdate) {
Expand All @@ -56,6 +57,7 @@ class UpdateAlert extends React.Component<Props, State> {
}

componentWillUnmount() {
console.log('UpdateAlert unmount')
ipcRenderer.removeAllListeners('update-needed')
ipcRenderer.removeAllListeners('update-check-err')
ipcRenderer.removeAllListeners('update-apply-err')
Expand All @@ -77,35 +79,60 @@ class UpdateAlert extends React.Component<Props, State> {
this.props.setUpdating()
}

openReleases = () => {
shell.openExternal('https://github.com/keys-pub/app/releases')
}

render() {
let action
let actionLabel

switch (platform()) {
case 'darwin':
action = this.apply
actionLabel = 'Download & Restart'
break
case 'win32':
action = this.apply
actionLabel = 'Download & Restart'
break
default:
action = this.openReleases
actionLabel = 'View Releases'
break
}

return (
<UpdateAlertView
open={this.state.open}
close={this.close}
version={this.state.version}
apply={this.apply}
action={action}
actionLabel={actionLabel}
/>
)
}
}

const UpdateAlertView = (props: {open: boolean; close: () => void; version: string; apply: () => void}) => {
type UpdateAlertProps = {
open: boolean
version: string
close: () => void
action: () => void
actionLabel: string
}

const UpdateAlertView = (props: UpdateAlertProps) => {
return (
<Snackbar
open={props.open}
onClose={() => {}} // Alert must be closed manually (not via clickaway or timeout)
anchorOrigin={{vertical: 'bottom', horizontal: 'left'}}
>
<Alert
onClose={props.close}
severity="info"
elevation={3}
// variant="filled"
style={{paddingTop: 5, paddingBottom: 3}}
>
<Alert onClose={props.close} severity="info" elevation={3} style={{paddingTop: 5, paddingBottom: 3}}>
<Box>
<Typography>There is a an update available ({props.version}).</Typography>
<Link onClick={props.apply}>Download &amp; Restart</Link>
<Link onClick={props.action}>{props.actionLabel}</Link>
</Box>
</Alert>
</Snackbar>
Expand Down

0 comments on commit e0cd267

Please sign in to comment.