Skip to content

Commit 8b42970

Browse files
committed
feat: Check for updates
1 parent 099c2f5 commit 8b42970

7 files changed

Lines changed: 105 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
publish: github

packages/neuron-wallet/src/controllers/app/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import { dialog, shell, Menu, MessageBoxOptions, SaveDialogOptions, BrowserWindow } from 'electron'
2+
import { dialog, shell, Menu, MenuItem, MessageBoxOptions, SaveDialogOptions, BrowserWindow } from 'electron'
33
import { take } from 'rxjs/operators'
44
import app from '../../app'
55
import { URL, contextMenuTemplate } from './options'
@@ -9,6 +9,7 @@ import NetworksService from '../../services/networks'
99
import WalletsService from '../../services/wallets'
1010
import WalletsController from '../wallets'
1111
import SyncInfoController from '../sync-info'
12+
import UpdateController from '../update'
1213

1314
import { Controller as ControllerDecorator } from '../../decorators'
1415
import { Channel, ResponseCode } from '../../utils/const'
@@ -155,6 +156,10 @@ export default class AppController {
155156
AppController.showMessageBox(options)
156157
}
157158

159+
public static checkUpdates(menuItem: MenuItem) {
160+
UpdateController.checkUpdates(menuItem)
161+
}
162+
158163
public static openWebsite() {
159164
AppController.openExternal(URL.Website)
160165
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { MenuItem, dialog } from 'electron'
2+
import { autoUpdater } from 'electron-updater'
3+
import i18n from '../utils/i18n'
4+
5+
autoUpdater.autoDownload = false
6+
7+
let updaterMenuItem: MenuItem | null
8+
const enableUpdaterMenuItem = () => {
9+
updaterMenuItem!.enabled = true
10+
updaterMenuItem = null
11+
}
12+
13+
autoUpdater.on('error', error => {
14+
dialog.showErrorBox('Error', error == null ? 'unknown' : (error.stack || error).toString())
15+
})
16+
17+
autoUpdater.on('update-available', () => {
18+
dialog.showMessageBox(
19+
{
20+
type: 'info',
21+
message: i18n.t('updater.updates-found-do-you-want-to-update'),
22+
buttons: [i18n.t('updater.update-now'), i18n.t('common.no')],
23+
},
24+
buttonIndex => {
25+
if (buttonIndex === 0) {
26+
autoUpdater.downloadUpdate()
27+
} else {
28+
enableUpdaterMenuItem()
29+
}
30+
}
31+
)
32+
})
33+
34+
autoUpdater.on('update-not-available', () => {
35+
dialog.showMessageBox({
36+
message: i18n.t('updater.update-not-available'),
37+
})
38+
enableUpdaterMenuItem()
39+
})
40+
41+
autoUpdater.on('update-downloaded', () => {
42+
dialog.showMessageBox(
43+
{
44+
message: i18n.t('updater.updates-downloaded-about-to-quit-and-install'),
45+
},
46+
() => {
47+
setImmediate(() => autoUpdater.quitAndInstall())
48+
}
49+
)
50+
})
51+
52+
export default class UpdateController {
53+
public static checkUpdates(menuItem: MenuItem) {
54+
updaterMenuItem = menuItem
55+
updaterMenuItem.enabled = false
56+
57+
autoUpdater.checkForUpdates()
58+
}
59+
}

packages/neuron-wallet/src/locales/en.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default {
99
neuron: {
1010
about: 'About {{app}}',
1111
preferences: 'Preferences...',
12+
'check-updates': 'Check for Updates...',
1213
quit: 'Quit {{app}}',
1314
},
1415
wallet: {
@@ -133,5 +134,16 @@ export default {
133134
cancel: 'Cancel',
134135
},
135136
},
137+
updater: {
138+
'update-not-available': 'There are currently no updates available.',
139+
'updates-found-do-you-want-to-update': 'An update is available, do you want to update now?',
140+
'update-now': 'Update now',
141+
'updates-downloaded-about-to-quit-and-install':
142+
'Update downloaded complete. Neuron will quit and install the the update...',
143+
},
144+
common: {
145+
yes: 'Yes',
146+
no: 'No',
147+
},
136148
},
137149
}

packages/neuron-wallet/src/locales/zh.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default {
99
neuron: {
1010
about: '关于{{app}}',
1111
preferences: '偏好设置...',
12+
'check-updates': '检查更新...',
1213
quit: '退出{{app}}',
1314
},
1415
wallet: {
@@ -132,5 +133,15 @@ export default {
132133
cancel: '取消',
133134
},
134135
},
136+
updater: {
137+
'update-not-available': '没有可供升级的新版本。',
138+
'updates-found-do-you-want-to-update': '有可供升级的新版本。马上进行升级吗?',
139+
'update-now': '马上升级',
140+
'updates-downloaded-about-to-quit-and-install': '下载完成。Neuron 将退出并安装新版本...',
141+
},
142+
common: {
143+
yes: '是',
144+
no: '否',
145+
},
135146
},
136147
}

packages/neuron-wallet/src/main.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { app } from 'electron'
2-
import { autoUpdater } from 'electron-updater'
32
import 'reflect-metadata'
43
import i18n from './utils/i18n'
54
import { updateApplicationMenu } from './utils/application-menu'
@@ -43,8 +42,6 @@ app.on('ready', async () => {
4342
await initConnection()
4443
createSyncBlockTask()
4544
openWindow()
46-
47-
autoUpdater.checkForUpdatesAndNotify()
4845
})
4946

5047
app.on('activate', openWindow)

packages/neuron-wallet/src/utils/application-menu.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ export const appMenuItem: MenuItemConstructorOptions = {
2727
}
2828
},
2929
},
30+
{
31+
label: i18n.t('application-menu.neuron.check-updates'),
32+
click: (menuItem: MenuItem) => {
33+
if (AppController) {
34+
AppController.checkUpdates(menuItem)
35+
}
36+
},
37+
},
3038
separator,
3139
{
3240
id: 'preference',
@@ -184,6 +192,14 @@ if (!isMac) {
184192
}
185193
},
186194
})
195+
helpSubmenu.push({
196+
label: i18n.t('application-menu.neuron.check-updates'),
197+
click: (menuItem: MenuItem) => {
198+
if (AppController) {
199+
AppController.checkUpdates(menuItem)
200+
}
201+
},
202+
})
187203
helpSubmenu.push({
188204
id: 'about',
189205
label: i18n.t('application-menu.neuron.about', {

0 commit comments

Comments
 (0)