fix: auto update

This commit is contained in:
kangfenmao 2024-07-14 15:44:13 +08:00
parent 71856a5cd5
commit 0556de81dd
8 changed files with 103 additions and 10 deletions

View File

@ -1,4 +1,4 @@
name: Build/Release Cherry Studio
name: Release
on:
push:
@ -55,7 +55,7 @@ jobs:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
- name: release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
with:
draft: false
files: |

View File

@ -1,3 +1,3 @@
provider: generic
url: https://example.com/auto-updates
url: http://127.0.0.1:8080
updaterCacheDirName: cherry-studio-updater

View File

@ -14,7 +14,7 @@ asarUnpack:
win:
executableName: Cherry Studio
nsis:
artifactName: ${name}-${version}-setup.${ext}
artifactName: Cherry-Studio-${version}-setup.${ext}
shortcutName: ${productName}
uninstallDisplayName: ${productName}
createDesktopShortcut: always
@ -27,7 +27,7 @@ mac:
- NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder.
notarize: false
dmg:
artifactName: ${name}-${version}.${ext}
artifactName: Cherry-Studio-${version}.${ext}
linux:
target:
- AppImage
@ -36,7 +36,7 @@ linux:
maintainer: electronjs.org
category: Utility
appImage:
artifactName: ${name}-${version}.${ext}
artifactName: Cherry-Studio-${version}.${ext}
npmRebuild: false
publish:
provider: github

View File

@ -23,6 +23,7 @@
"dependencies": {
"@electron-toolkit/preload": "^3.0.0",
"@electron-toolkit/utils": "^3.0.0",
"electron-log": "^5.1.5",
"electron-updater": "^6.1.7",
"electron-window-state": "^5.0.3"
},

View File

@ -4,7 +4,7 @@ import windowStateKeeper from 'electron-window-state'
import { join } from 'path'
import icon from '../../resources/icon.png?asset'
import installExtension, { REDUX_DEVTOOLS } from 'electron-devtools-installer'
import { autoUpdater } from 'electron-updater'
import AppUpdater from './updater'
function createWindow(): void {
// Load the previous state with fallback to defaults
@ -94,7 +94,7 @@ app.whenReady().then(() => {
.then((name) => console.log(`Added Extension: ${name}`))
.catch((err) => console.log('An error occurred: ', err))
autoUpdater.checkForUpdatesAndNotify()
setTimeout(() => new AppUpdater(), 5000)
})
// Quit when all windows are closed, except on macOS. There, it's common

84
src/main/updater.ts Normal file
View File

@ -0,0 +1,84 @@
import { autoUpdater, UpdateInfo } from 'electron-updater'
import logger from 'electron-log'
import { dialog, ipcMain } from 'electron'
export default class AppUpdater {
constructor() {
logger.transports.file.level = 'debug'
autoUpdater.logger = logger
autoUpdater.forceDevUpdateConfig = true
autoUpdater.autoDownload = false
autoUpdater.checkForUpdatesAndNotify()
// 触发检查更新(此方法用于被渲染线程调用,例如页面点击检查更新按钮来调用此方法)
ipcMain.on('check-for-update', () => {
logger.info('触发检查更新')
autoUpdater.checkForUpdates()
})
// 检测下载错误
autoUpdater.on('error', (error) => {
logger.error('更新异常', error)
})
// 检测是否需要更新
autoUpdater.on('checking-for-update', () => {
logger.info('正在检查更新……')
})
autoUpdater.on('update-available', (releaseInfo: UpdateInfo) => {
autoUpdater.logger?.info('检测到新版本,确认是否下载')
const releaseNotes = releaseInfo.releaseNotes
let releaseContent = ''
if (releaseNotes) {
if (typeof releaseNotes === 'string') {
releaseContent = <string>releaseNotes
} else if (releaseNotes instanceof Array) {
releaseNotes.forEach((releaseNote) => {
releaseContent += `${releaseNote}\n`
})
}
} else {
releaseContent = '暂无更新说明'
}
// 弹框确认是否下载更新releaseContent是更新日志
dialog
.showMessageBox({
type: 'info',
title: '应用有新的更新',
detail: releaseContent,
message: '发现新版本,是否现在更新?',
buttons: ['否', '是']
})
.then(({ response }) => {
if (response === 1) {
autoUpdater.downloadUpdate()
}
})
})
// 检测到不需要更新时
autoUpdater.on('update-not-available', () => {
logger.info('现在使用的就是最新版本,不用更新')
})
// 更新下载进度
autoUpdater.on('download-progress', (progress) => {
logger.info('下载进度', progress)
})
// 当需要更新的内容下载完成后
autoUpdater.on('update-downloaded', () => {
logger.info('下载完成,准备更新')
dialog
.showMessageBox({
title: '安装更新',
message: '更新下载完毕,应用将重启并进行安装'
})
.then(() => {
setImmediate(() => autoUpdater.quitAndInstall())
})
})
}
}

View File

@ -6,7 +6,7 @@ function init() {
name: 'CherryAI',
version: 1.0,
storeName: 'cherryai',
description: 'Cherry Studio storage'
description: 'Cherry Studio Storage'
})
}

View File

@ -2642,6 +2642,7 @@ __metadata:
electron: "npm:^28.2.0"
electron-builder: "npm:^24.9.1"
electron-devtools-installer: "npm:^3.2.0"
electron-log: "npm:^5.1.5"
electron-updater: "npm:^6.1.7"
electron-vite: "npm:^2.0.0"
electron-window-state: "npm:^5.0.3"
@ -3251,6 +3252,13 @@ __metadata:
languageName: node
linkType: hard
"electron-log@npm:^5.1.5":
version: 5.1.5
resolution: "electron-log@npm:5.1.5"
checksum: 10c0/40bb497d1a515855d52d49ec86dee55b1eddf990b8cf74b24e79b1af5830413fd47afb8c060b830201e3b7d2c5f5401f5cc5c2321e91ab0c66fc8e862c9b8380
languageName: node
linkType: hard
"electron-publish@npm:24.13.1":
version: 24.13.1
resolution: "electron-publish@npm:24.13.1"