fix: check for updates

This commit is contained in:
idranme 2024-09-12 00:58:59 +08:00
parent 2ac2c68435
commit 1132495eb3
No known key found for this signature in database
GPG Key ID: 926F7B5B668E495F

View File

@ -5,20 +5,21 @@ import { version } from '../../version'
import { copyFolder, log, fetchFile } from '.' import { copyFolder, log, fetchFile } from '.'
import { PLUGIN_DIR, TEMP_DIR } from '../globalVars' import { PLUGIN_DIR, TEMP_DIR } from '../globalVars'
const downloadMirrorHosts = ['https://mirror.ghproxy.com/'] const downloadMirrorHosts = ['https://ghp.ci/']
const checkVersionMirrorHosts = ['https://kkgithub.com']
export async function checkNewVersion() { export async function checkNewVersion() {
const latestVersionText = await getRemoteVersion() const latestVersionText = await getRemoteVersion()
const latestVersion = latestVersionText.split('.') const latestVersion = latestVersionText.split('.')
//log('llonebot last version', latestVersion) log('LLOneBot latest version', latestVersion)
const currentVersion: string[] = version.split('.') const currentVersion = version.split('.')
//log('llonebot current version', currentVersion) //log('llonebot current version', currentVersion)
for (const k of [0, 1, 2]) { for (const k of [0, 1, 2]) {
if (parseInt(latestVersion[k]) > parseInt(currentVersion[k])) { const latest = parseInt(latestVersion[k])
const current = parseInt(currentVersion[k])
if (latest > current) {
log('') log('')
return { result: true, version: latestVersionText } return { result: true, version: latestVersionText }
} else if (parseInt(latestVersion[k]) < parseInt(currentVersion[k])) { } else if (latest < current) {
break break
} }
} }
@ -28,7 +29,7 @@ export async function checkNewVersion() {
export async function upgradeLLOneBot() { export async function upgradeLLOneBot() {
const latestVersion = await getRemoteVersion() const latestVersion = await getRemoteVersion()
if (latestVersion && latestVersion != '') { if (latestVersion && latestVersion != '') {
const downloadUrl = 'https://github.com/LLOneBot/LLOneBot/releases/download/v' + latestVersion + '/LLOneBot.zip' const downloadUrl = `https://github.com/LLOneBot/LLOneBot/releases/download/v${latestVersion}/LLOneBot.zip`
const filePath = path.join(TEMP_DIR, './update-' + latestVersion + '.zip') const filePath = path.join(TEMP_DIR, './update-' + latestVersion + '.zip')
let downloadSuccess = false let downloadSuccess = false
// 多镜像下载 // 多镜像下载
@ -73,26 +74,21 @@ export async function upgradeLLOneBot() {
} }
export async function getRemoteVersion() { export async function getRemoteVersion() {
let Version = '' let version = ''
for (let i = 0; i < checkVersionMirrorHosts.length; i++) { const mirrorGithub = downloadMirrorHosts[0]
const mirrorGithub = checkVersionMirrorHosts[i] const tVersion = await getRemoteVersionByMirror(mirrorGithub)
const tVersion = await getRemoteVersionByMirror(mirrorGithub) if (tVersion) {
if (tVersion && tVersion != '') { version = tVersion
Version = tVersion
break
}
} }
return Version return version
} }
export async function getRemoteVersionByMirror(mirrorGithub: string) { export async function getRemoteVersionByMirror(mirrorGithub: string) {
let releasePage = 'error'
try { try {
releasePage = (await fetchFile(mirrorGithub + '/LLOneBot/LLOneBot/releases')).data.toString() const source = 'https://raw.githubusercontent.com/LLOneBot/LLOneBot/main/src/version.ts'
// log("releasePage", releasePage); const page = (await fetchFile(mirrorGithub + source)).data.toString()
if (releasePage === 'error') return '' return page.match(/(\d+\.\d+\.\d+)/)?.[0]
return releasePage.match(new RegExp('(?<=(tag/v)).*?(?=("))'))?.[0] } catch (e) {
} catch { } log(e?.toString())
return '' }
} }