Skip to content

Commit

Permalink
fix: auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
kangfenmao committed Jul 14, 2024
1 parent 6d82065 commit c05698b
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build/Release Cherry Studio
name: Release

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion dev-app-update.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
provider: generic
url: https://example.com/auto-updates
url: http://127.0.0.1:8080
updaterCacheDirName: cherry-studio-updater
8 changes: 4 additions & 4 deletions electron-builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ files:
asarUnpack:
- resources/**
win:
executableName: CherryStudio
executableName: Cherry Studio
nsis:
artifactName: ${name}-${version}-setup.${ext}
artifactName: ${productName}-${version}-setup.${ext}
shortcutName: ${productName}
uninstallDisplayName: ${productName}
createDesktopShortcut: always
Expand All @@ -27,7 +27,7 @@ mac:
- NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder.
notarize: false
dmg:
artifactName: ${name}-${version}.${ext}
artifactName: ${productName}-${version}.${ext}
linux:
target:
- AppImage
Expand All @@ -36,7 +36,7 @@ linux:
maintainer: electronjs.org
category: Utility
appImage:
artifactName: ${name}-${version}.${ext}
artifactName: ${productName}-${version}.${ext}
npmRebuild: false
publish:
provider: github
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
84 changes: 84 additions & 0 deletions src/main/updater.ts
Original file line number Diff line number Diff line change
@@ -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())
})
})
}
}
2 changes: 1 addition & 1 deletion src/renderer/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function init() {
name: 'CherryAI',
version: 1.0,
storeName: 'cherryai',
description: 'Cherry Studio storage'
description: 'Cherry Studio Storage'
})
}

Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit c05698b

Please sign in to comment.