improve checkUpdate

This commit is contained in:
josc146 2023-05-29 21:15:40 +08:00
parent deb9e030cb
commit 154827f367

View File

@ -214,8 +214,7 @@ export function bytesToKb(size: number) {
} }
export async function checkUpdate(notifyEvenLatest: boolean = false) { export async function checkUpdate(notifyEvenLatest: boolean = false) {
let updateUrl = ''; fetch(!commonStore.settings.giteeUpdatesSource ?
await fetch(!commonStore.settings.giteeUpdatesSource ?
'https://api.github.com/repos/josstorer/RWKV-Runner/releases/latest' : 'https://api.github.com/repos/josstorer/RWKV-Runner/releases/latest' :
'https://gitee.com/api/v5/repos/josc146/RWKV-Runner/releases/latest' 'https://gitee.com/api/v5/repos/josc146/RWKV-Runner/releases/latest'
).then((r) => { ).then((r) => {
@ -224,28 +223,45 @@ export async function checkUpdate(notifyEvenLatest: boolean = false) {
if (data.tag_name) { if (data.tag_name) {
const versionTag = data.tag_name; const versionTag = data.tag_name;
if (versionTag.replace('v', '') > manifest.version) { if (versionTag.replace('v', '') > manifest.version) {
updateUrl = !commonStore.settings.giteeUpdatesSource ? const verifyUrl = !commonStore.settings.giteeUpdatesSource ?
`https://github.com/josStorer/RWKV-Runner/releases/download/${versionTag}/RWKV-Runner_windows_x64.exe` : `https://api.github.com/repos/josstorer/RWKV-Runner/releases/tags/${versionTag}` :
`https://gitee.com/josc146/RWKV-Runner/releases/download/${versionTag}/RWKV-Runner_windows_x64.exe`; `https://gitee.com/api/v5/repos/josc146/RWKV-Runner/releases/tags/${versionTag}`;
toastWithButton(t('New Version Available') + ': ' + versionTag, t('Update'), () => {
DeleteFile('cache.json'); fetch(verifyUrl).then((r) => {
toast(t('Downloading update, please wait. If it is not completed, please manually download the program from GitHub and replace the original program.'), { if (r.ok) {
type: 'info', r.json().then((data) => {
position: 'bottom-left', if (data.assets && data.assets.length > 0) {
autoClose: 10000 const asset = data.assets.find((a: any) => a.name.toLowerCase().includes(commonStore.platform.toLowerCase()));
}); if (asset) {
setTimeout(() => { const updateUrl = !commonStore.settings.giteeUpdatesSource ?
UpdateApp(updateUrl).catch((e) => { `https://github.com/josStorer/RWKV-Runner/releases/download/${versionTag}/${asset.name}` :
toast(t('Update Error') + ' - ' + e.message || e, { `https://gitee.com/josc146/RWKV-Runner/releases/download/${versionTag}/${asset.name}`;
type: 'error', toastWithButton(t('New Version Available') + ': ' + versionTag, t('Update'), () => {
position: 'bottom-left', DeleteFile('cache.json');
autoClose: false toast(t('Downloading update, please wait. If it is not completed, please manually download the program from GitHub and replace the original program.'), {
}); type: 'info',
position: 'bottom-left',
autoClose: 10000
});
setTimeout(() => {
UpdateApp(updateUrl).catch((e) => {
toast(t('Update Error') + ' - ' + e.message || e, {
type: 'error',
position: 'bottom-left',
autoClose: false
});
});
}, 500);
}, {
autoClose: false,
position: 'bottom-left'
});
}
}
}); });
}, 500); } else {
}, { throw new Error('Verify response was not ok.');
autoClose: false, }
position: 'bottom-left'
}); });
} else { } else {
if (notifyEvenLatest) { if (notifyEvenLatest) {
@ -263,7 +279,6 @@ export async function checkUpdate(notifyEvenLatest: boolean = false) {
).catch((e) => { ).catch((e) => {
toast(t('Updates Check Error') + ' - ' + e.message || e, { type: 'error', position: 'bottom-left' }); toast(t('Updates Check Error') + ' - ' + e.message || e, { type: 'error', position: 'bottom-left' });
}); });
return updateUrl;
} }
export function toastWithButton(text: string, buttonText: string, onClickButton: () => void, options?: ToastOptions) { export function toastWithButton(text: string, buttonText: string, onClickButton: () => void, options?: ToastOptions) {