Skip to content

Commit

Permalink
refeator(updateChecker):fix some bugs and refeator code
Browse files Browse the repository at this point in the history
  • Loading branch information
ni00 committed Jun 5, 2023
1 parent 802ea60 commit 8fd2f15
Showing 1 changed file with 94 additions and 59 deletions.
153 changes: 94 additions & 59 deletions src/main/updateChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,124 @@ 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'
interface versionDetail {
version: string
detail: string
}

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
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
}
return language === 'zh' ? 'zh' : 'en'
}

for (let i: number = 0; i < 3; i++) {
if (currentVersion[i] < latestVersion[i]) {
update = true
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://api.github.com/repos/emqx/MQTTX/releases/tags/${latestVersion}`)
if (versionRes.status === 200 && !versionRes.data.prerelease) {
return {
version: versionRes.data.name,
detail: versionRes.data.body,
}
}
}
}

return update
return null
}

const autoDownload = (latest: string, language: string): void => {
const urlLang = language === 'zh' ? 'zh' : 'en'
const downloadUrl = `https://www.emqx.com/${urlLang}/downloads/MQTTX/${latest}`
const autoDownload = (currentVersion: string, updateDatail: versionDetail, language: string): void => {
let msgClick = false
const downloadUrl = `https://www.emqx.com/${language}/downloads/MQTTX/${updateDatail.version}`
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)
autoUpdater
.checkForUpdatesAndNotify()
.then((r: any) => {})
.catch((e: any) => console.log(e))
autoUpdater.on('update-available', () => {
// TODO: Replace dialog.showMessageBox with BrowserWindow and display the update logs (versionDetail.detail).
dialog
.showMessageBox({
type: 'info',
title: 'New Version',
buttons: ['Install', 'No'],
message: `Update available: ${latest}`,
title: `Update Infomation ${currentVersion}->${updateDatail.version}`,
message: `The software needs to be updated. Do you want to update it immediately?\n${updateDatail.detail}`,
buttons: ['Update now', "Don't remind me", 'Remind me next time', 'Ignore this version update'],
})
.then((res) => {
if (res.response === 0) {
// if selected yes
autoUpdater.quitAndInstall()
msgClick = true
autoUpdater.downloadUpdate()
} else if (res.response === 1) {
// TODO: Change the value of "autocheck" in "settings" to false.
} else if (res.response === 2) {
autoUpdater.autoDownload = false
} else {
dialog.showMessageBox({
type: 'info',
message: 'Automatic update on do not shut down the computer immediately',
})
electronStore.set('isIgnore', updateDatail.version)
}
})
})
autoUpdater.on('error', (e: any) => {
console.log(e)
})
autoUpdater.on('download-progress', (progressObj: any) => {
// TODO:Add a progress bar for updates.
//progressWindow.webContents.send("downloadProgress", progressObj);
})
autoUpdater.on('update-downloaded', () => {
if (msgClick) {
dialog
.showMessageBox({
type: 'info',
title: 'New Version',
buttons: ['Install', 'No'],
message: `Update available: ${updateDatail.version}`,
})
.then((res) => {
if (res.response === 0) {
electronStore.set('isShow', true)
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)
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.',
})
}
const currentVersion = `v${version}`
const updateDatail: versionDetail | null = await getUpdateDtail(currentVersion)
const language: string = await getCurrentLang()
if (updateDatail) {
if (isAuto && electronStore.get('isIgnore') === updateDatail.version) {
return false
}
autoDownload(currentVersion, updateDatail, language)
} else if (!isAuto) {
dialog.showMessageBox({
type: 'info',
title: '',
buttons: ['OK'],
message: 'There are currently no updates available.',
})
} else {
return false
}
}
//what's new window

export async function createUpdateWindow() {
const updateWindow = new BrowserWindow({
width: 600,
Expand All @@ -95,12 +135,7 @@ export async function createUpdateWindow() {
enableRemoteModule: true,
},
})
const { settingService } = useServices()
await settingService.set()
const setting = await settingService.get()
if (setting) {
language = setting.currentLang
}
const language: string = await getCurrentLang()
let link: string = 'https://mqttx.app'
link = language === 'zh' ? `${link}/zh` : link
updateWindow.loadURL(`${link}/changelogs/v${version}`)
Expand Down

0 comments on commit 8fd2f15

Please sign in to comment.