Perf: remove duplicate check of new version, refactor getLatestVersion func

This commit is contained in:
萌萌哒赫萝 2023-04-16 22:48:34 +08:00
parent 15d34ac90a
commit ef917ce26e
2 changed files with 18 additions and 21 deletions

View File

@ -1,9 +1,5 @@
import db from '~/main/apis/core/datastore' import db from '~/main/apis/core/datastore'
import { getLatestVersion } from '#/utils/getLatestVersion'
import { autoUpdater } from 'electron-updater' import { autoUpdater } from 'electron-updater'
// const releaseUrl = 'https://api.github.com/repos/Molunerfinn/PicGo/releases'
// const releaseUrlBackup = 'https://picgo-1251750343.cos.ap-chengdu.myqcloud.com'
// const downloadUrl = 'https://github.com/Kuingsmile/PicList/releases/latest'
const checkVersion = async () => { const checkVersion = async () => {
let showTip = db.get('settings.showUpdateTip') let showTip = db.get('settings.showUpdateTip')
@ -12,10 +8,9 @@ const checkVersion = async () => {
showTip = true showTip = true
} }
if (showTip) { if (showTip) {
const res: string = await getLatestVersion() try {
if (res !== '') { await autoUpdater.checkForUpdatesAndNotify()
autoUpdater.checkForUpdatesAndNotify() } catch (err) {
} else {
return false return false
} }
} else { } else {

View File

@ -1,21 +1,23 @@
import axios from 'axios' import axios from 'axios'
import { RELEASE_URL, RELEASE_URL_BACKUP } from './static'
import yaml from 'js-yaml' import yaml from 'js-yaml'
import { RELEASE_URL, RELEASE_URL_BACKUP } from './static'
export const getLatestVersion = async () => { export const getLatestVersion = async (): Promise<string> => {
let res: string = ''
try { try {
res = await axios.get(RELEASE_URL).then(r => { const { data } = await axios.get(RELEASE_URL)
const list = r.data as IStringKeyMap[] const releases = data as IStringKeyMap[]
const normalList = list.filter(item => !item.name.includes('beta')) const normalList = releases.filter(item => !item.name.includes('beta'))
return normalList[0].name const latestRelease = normalList[0]
}).catch(async () => { return latestRelease.name
const result = await axios.get(`${RELEASE_URL_BACKUP}/latest.yml`)
const r = yaml.load(result.data) as IStringKeyMap
return r.version
})
} catch (err) { } catch (err) {
console.log(err) console.log(err)
try {
const { data } = await axios.get(`${RELEASE_URL_BACKUP}/latest.yml`)
const r = yaml.load(data) as IStringKeyMap
return r.version
} catch (err) {
console.log(err)
return ''
}
} }
return res
} }