-
Notifications
You must be signed in to change notification settings - Fork 444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(updateChecker):optimize automatic updates and add a progress bar #1314
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export default { | ||
updateTitle: { | ||
zh: '有可用更新', | ||
en: 'Update Available', | ||
ja: '利用可能な更新', | ||
tr: 'Güncelleme Mevcut', | ||
hu: 'Frissítés elérhető', | ||
}, | ||
ignoreVersion: { | ||
zh: '忽略', | ||
en: 'Dismiss', | ||
ja: '却下', | ||
tr: 'Reddet', | ||
hu: 'Elutasít', | ||
}, | ||
nextRemind: { | ||
zh: '稍后提醒', | ||
en: 'Remind me Later', | ||
ja: '後でリマインドしてください', | ||
tr: 'Bana daha sonra hatırlat', | ||
hu: 'Emlékeztessen később', | ||
}, | ||
update: { | ||
zh: '更新', | ||
en: 'Update', | ||
ja: '更新', | ||
tr: 'Güncelle', | ||
hu: 'Frissítés', | ||
}, | ||
downloadProgress: { | ||
zh: '下载进度', | ||
en: 'Download Progress', | ||
ja: 'ダウンロード進行状況', | ||
tr: 'İndirme İlerlemesi', | ||
hu: 'Letöltési Előrehaladás', | ||
}, | ||
downloading: { | ||
zh: '下载更新中...', | ||
en: 'Downloading Update...', | ||
ja: '更新をダウンロード中...', | ||
tr: 'Güncelleme indiriliyor...', | ||
hu: 'Frissítés letöltése...', | ||
}, | ||
downloaded: { | ||
zh: '安装并重启', | ||
en: 'Restart and Install Update', | ||
ja: '再起動してアップデートをインストール', | ||
tr: 'Yeniden Başlat ve Güncellemeyi Yükle', | ||
hu: 'Újraindítás és Frissítés Telepítése', | ||
}, | ||
cancel: { | ||
zh: '取消更新', | ||
en: 'Cancel Update', | ||
ja: '更新をキャンセル', | ||
tr: 'Güncellemeyi iptal et', | ||
hu: 'Frissítés megszakítása', | ||
}, | ||
install: { | ||
zh: '安装并重启', | ||
en: 'Install and Relunch', | ||
ja: 'インストールして再起動', | ||
tr: 'Kur ve Tekrar Başlat', | ||
hu: 'Telepítés és újraindítás', | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,54 @@ | ||
import { dialog, BrowserWindow } from 'electron' | ||
import axios from 'axios' | ||
import version from '@/version' | ||
import useServices from '@/database/useServices' | ||
|
||
const { autoUpdater } = require('electron-updater') | ||
const Store = require('electron-store') | ||
const electronStore = new Store() | ||
|
||
const release = 'https://api.github.com/repos/emqx/MQTTX/releases/latest' | ||
let language: string = 'en' | ||
|
||
const isUpdate = (latest: string, current: string): boolean => { | ||
const latestVersion: number[] = latest.split('.').map((item) => parseInt(item, 10)) | ||
const currentVersion: number[] = current.split('.').map((item) => parseInt(item, 10)) | ||
let update: boolean = false | ||
|
||
for (let i: number = 0; i < 3; i++) { | ||
if (currentVersion[i] < latestVersion[i]) { | ||
update = true | ||
} | ||
} | ||
|
||
return update | ||
export interface versionDetail { | ||
version: string | ||
detail: string | ||
} | ||
|
||
const autoDownload = (latest: string, language: string): void => { | ||
const urlLang = language === 'zh' ? 'zh' : 'en' | ||
const downloadUrl = `https://www.emqx.com/${urlLang}/downloads/MQTTX/${latest}` | ||
autoUpdater.setFeedURL(downloadUrl) | ||
autoUpdater.checkForUpdatesAndNotify() | ||
autoUpdater.on('checking-for-update', () => {}) | ||
autoUpdater.on('update-available', () => {}) | ||
autoUpdater.on('update-not-available', () => {}) | ||
autoUpdater.on('error', () => {}) | ||
autoUpdater.on('download-progress', () => {}) | ||
autoUpdater.on('update-downloaded', () => { | ||
electronStore.set('isShow', true) | ||
dialog | ||
.showMessageBox({ | ||
type: 'info', | ||
title: 'New Version', | ||
buttons: ['Install', 'No'], | ||
message: `Update available: ${latest}`, | ||
}) | ||
.then((res) => { | ||
if (res.response === 0) { | ||
// if selected yes | ||
autoUpdater.quitAndInstall() | ||
} else { | ||
dialog.showMessageBox({ | ||
type: 'info', | ||
message: 'Automatic update on do not shut down the computer immediately', | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
const updateChecker = async (isAuto: boolean = true): Promise<void | boolean> => { | ||
const response = await axios.get(release) | ||
export const getCurrentLang = async (): Promise<string> => { | ||
let language: string = 'en' | ||
const { settingService } = useServices() | ||
await settingService.set() | ||
const setting = await settingService.get() | ||
if (setting) { | ||
language = setting.currentLang | ||
} | ||
if (response.status === 200) { | ||
const latest: string = response.data.name | ||
const isPrerelease: boolean = response.data.prerelease | ||
if (latest && isUpdate(latest.slice(1, 6), version) && !isPrerelease) { | ||
autoDownload(latest, language) | ||
} else { | ||
if (!isAuto) { | ||
dialog.showMessageBox({ | ||
type: 'info', | ||
title: '', | ||
buttons: ['OK'], | ||
message: 'There are currently no updates available.', | ||
}) | ||
return language === 'zh' ? 'zh' : 'en' | ||
} | ||
|
||
const getUpdateDtail = async (current: string): Promise<versionDetail | null> => { | ||
const tagsUrl = 'https://api.github.com/repos/emqx/MQTTX/tags' | ||
const tagsRes = await axios.get(tagsUrl) | ||
if (tagsRes.status === 200) { | ||
const tagsList: string[] = tagsRes.data.map((item: any) => item.name) | ||
const latestTagsList: string[] = tagsList.slice(0, tagsList.indexOf(current)) | ||
while (latestTagsList.length > 0) { | ||
const latestVersion = latestTagsList.shift() | ||
const versionRes = await axios.get( | ||
`https://community-sites.emqx.com/api/v1/changelogs?product=MQTTX&version=${latestVersion}`, | ||
) | ||
if (latestVersion && versionRes.status === 200) { | ||
return { | ||
version: latestVersion, | ||
detail: versionRes.data.data.changelog, | ||
} | ||
} | ||
} | ||
} else { | ||
return false | ||
} | ||
return null | ||
} | ||
//what's new window | ||
export async function createUpdateWindow() { | ||
const updateWindow = new BrowserWindow({ | ||
width: 600, | ||
height: 500, | ||
webPreferences: { | ||
nodeIntegration: true, | ||
contextIsolation: false, | ||
enableRemoteModule: true, | ||
}, | ||
}) | ||
const { settingService } = useServices() | ||
await settingService.set() | ||
const setting = await settingService.get() | ||
if (setting) { | ||
language = setting.currentLang | ||
|
||
export const updateChecker = async (isAuto: boolean = true): Promise<void | versionDetail | boolean> => { | ||
const currentVersion = `v${version}` | ||
const updateDetail: versionDetail | null = await getUpdateDtail(currentVersion) | ||
const language: string = await getCurrentLang() | ||
if (updateDetail && (!isAuto || electronStore.get('isIgnore') !== updateDetail.version)) { | ||
return updateDetail | ||
} | ||
let link: string = 'https://mqttx.app' | ||
link = language === 'zh' ? `${link}/zh` : link | ||
updateWindow.loadURL(`${link}/changelogs/v${version}`) | ||
electronStore.set('isShow', false) | ||
return false | ||
} | ||
export default updateChecker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { BrowserWindow, IpcMainEvent, ipcMain } from 'electron' | ||
import version from '@/version' | ||
import { getCurrentLang, versionDetail } from './updateChecker' | ||
|
||
const { autoUpdater } = require('electron-updater') | ||
const Store = require('electron-store') | ||
const electronStore = new Store() | ||
|
||
export const autoDownload = (event: IpcMainEvent, updateDetail: versionDetail, language: string) => { | ||
const downloadUrl = `https://www.emqx.com/${language}/downloads/MQTTX/${updateDetail.version}` | ||
autoUpdater.setFeedURL(downloadUrl) | ||
autoUpdater.autoDownload = false | ||
autoUpdater.autoInstallOnAppQuit = false | ||
autoUpdater.checkForUpdatesAndNotify() | ||
autoUpdater.on('update-available', () => { | ||
autoUpdater.downloadUpdate() | ||
}) | ||
autoUpdater.on('error', (e: any) => { | ||
console.log(e) | ||
}) | ||
autoUpdater.on('download-progress', (progressObj: any) => { | ||
event.sender.send('downloadProgressPercent', progressObj.percent) | ||
}) | ||
autoUpdater.on('update-downloaded', () => { | ||
event.sender.send('downloadProgressPercent', 100) | ||
}) | ||
ipcMain.on('toQuitAndInstall', () => { | ||
electronStore.set('isShow', true) | ||
autoUpdater.quitAndInstall() | ||
}) | ||
ipcMain.on('cancelDownload', () => { | ||
autoUpdater.removeAllListeners() | ||
autoUpdater.autoDownload = false | ||
autoUpdater.autoInstallOnAppQuit = false | ||
autoUpdater.autoCheckForUpdates = false | ||
}) | ||
} | ||
|
||
export async function createUpdateWindow() { | ||
const updateWindow = new BrowserWindow({ | ||
width: 600, | ||
height: 500, | ||
webPreferences: { | ||
nodeIntegration: true, | ||
contextIsolation: false, | ||
enableRemoteModule: true, | ||
}, | ||
}) | ||
const language: string = await getCurrentLang() | ||
let link: string = 'https://mqttx.app' | ||
link = language === 'zh' ? `${link}/zh` : link | ||
updateWindow.loadURL(`${link}/changelogs/v${version}`) | ||
electronStore.set('isShow', false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an update window size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes