From ef917ce26eefc3e84a59c5e0c7e658dba9224d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E8=90=8C=E5=93=92=E8=B5=AB=E8=90=9D?= Date: Sun, 16 Apr 2023 22:48:34 +0800 Subject: [PATCH] :zap: Perf: remove duplicate check of new version, refactor getLatestVersion func --- src/main/utils/updateChecker.ts | 11 +++------- src/universal/utils/getLatestVersion.ts | 28 +++++++++++++------------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/main/utils/updateChecker.ts b/src/main/utils/updateChecker.ts index bf121477..2623c9e6 100644 --- a/src/main/utils/updateChecker.ts +++ b/src/main/utils/updateChecker.ts @@ -1,9 +1,5 @@ import db from '~/main/apis/core/datastore' -import { getLatestVersion } from '#/utils/getLatestVersion' 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 () => { let showTip = db.get('settings.showUpdateTip') @@ -12,10 +8,9 @@ const checkVersion = async () => { showTip = true } if (showTip) { - const res: string = await getLatestVersion() - if (res !== '') { - autoUpdater.checkForUpdatesAndNotify() - } else { + try { + await autoUpdater.checkForUpdatesAndNotify() + } catch (err) { return false } } else { diff --git a/src/universal/utils/getLatestVersion.ts b/src/universal/utils/getLatestVersion.ts index 321cc62a..a8e41069 100644 --- a/src/universal/utils/getLatestVersion.ts +++ b/src/universal/utils/getLatestVersion.ts @@ -1,21 +1,23 @@ import axios from 'axios' -import { RELEASE_URL, RELEASE_URL_BACKUP } from './static' import yaml from 'js-yaml' +import { RELEASE_URL, RELEASE_URL_BACKUP } from './static' -export const getLatestVersion = async () => { - let res: string = '' +export const getLatestVersion = async (): Promise => { try { - res = await axios.get(RELEASE_URL).then(r => { - const list = r.data as IStringKeyMap[] - const normalList = list.filter(item => !item.name.includes('beta')) - return normalList[0].name - }).catch(async () => { - const result = await axios.get(`${RELEASE_URL_BACKUP}/latest.yml`) - const r = yaml.load(result.data) as IStringKeyMap - return r.version - }) + const { data } = await axios.get(RELEASE_URL) + const releases = data as IStringKeyMap[] + const normalList = releases.filter(item => !item.name.includes('beta')) + const latestRelease = normalList[0] + return latestRelease.name } catch (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 }