app自动更新逻辑

This commit is contained in:
Soviby 2024-10-07 23:24:55 +08:00
parent f4a93680cf
commit d14162bf74
6 changed files with 72 additions and 9 deletions

View File

@ -2,6 +2,7 @@
"name": "electron-app-c1-launcher", "name": "electron-app-c1-launcher",
"version": "0.0.0", "version": "0.0.0",
"private": true, "private": true,
"author": "soviby 936858871@qq.com",
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "run-p type-check \"build-only {@}\" --", "build": "run-p type-check \"build-only {@}\" --",
@ -10,6 +11,8 @@
"type-check": "vue-tsc --build --force" "type-check": "vue-tsc --build --force"
}, },
"dependencies": { "dependencies": {
"electron-log": "^5.2.0",
"electron-updater": "^6.3.9",
"vue": "^3.4.29" "vue": "^3.4.29"
}, },
"devDependencies": { "devDependencies": {

View File

@ -42,10 +42,14 @@ export const ElectronBuildPlugin = (): Plugin => {
icon: path.resolve(process.cwd(), 'src/assets/test-icon.ico'), // 指定图标文件路径 icon: path.resolve(process.cwd(), 'src/assets/test-icon.ico'), // 指定图标文件路径
target: [ target: [
{ {
target: 'portable', target: 'nsis', // 安装模式: nsis 单文件启动: portable
arch: ['x64', 'ia32'] arch: ['x64']
} }
] ]
},
nsis: {
oneClick: false,
allowToChangeInstallationDirectory: true
} }
} }
}) })

View File

@ -5,7 +5,6 @@ import TheWelcome from './components/TheWelcome.vue'
<template> <template>
<header> <header>
<h1> Hello Soviby! </h1>
<img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" /> <img alt="Vue logo" class="logo" src="./assets/logo.svg" width="125" height="125" />
<div class="wrapper"> <div class="wrapper">

View File

@ -1,11 +1,23 @@
// electron 主进程文件 // electron 主进程文件
import { app, BrowserWindow} from 'electron' import { app, BrowserWindow } from 'electron'
import log from './logger'
import AppUpdater from './electron_updater'
app.whenReady().then(()=>{ let appUpdater: AppUpdater
process.on('unhandledRejection', (reason, promise) => {
log.error('Unhandled Rejection at:', promise, 'reason:', reason);
})
process.on('uncaughtException', (error) => {
log.error('Uncaught Exception:', error);
})
app.whenReady().then(() => {
const win = new BrowserWindow({ const win = new BrowserWindow({
height: 600, height: 600,
width: 800, width: 800,
webPreferences:{ webPreferences: {
nodeIntegration: true, // 可以在渲染进程汇总使用node的api为了安全默认false nodeIntegration: true, // 可以在渲染进程汇总使用node的api为了安全默认false
contextIsolation: false, // 关闭渲染进程的沙箱 contextIsolation: false, // 关闭渲染进程的沙箱
webSecurity: false, // 关闭跨域检测 webSecurity: false, // 关闭跨域检测
@ -13,14 +25,16 @@ app.whenReady().then(()=>{
}) })
// win.webContents.openDevTools() // 开发环境打开控制台 // win.webContents.openDevTools() // 开发环境打开控制台
if(process.argv[2]) // 开发环境 if (process.argv[2]) // 开发环境
{ {
win.loadURL(process.argv[2]) win.loadURL(process.argv[2])
} }
else // 生产环境 else // 生产环境
{ {
// win.loadFile('index.html') // 自动更新
appUpdater = new AppUpdater()
win.loadFile('index.html')
// 这里换成静态html后即可实现ui热更新 // 这里换成静态html后即可实现ui热更新
win.loadURL('http://49.235.154.178:20081/index.html') // win.loadURL('http://49.235.154.178:20081/index.html')
} }
}) })

23
src/electron_updater.ts Normal file
View File

@ -0,0 +1,23 @@
import { NsisUpdater } from "electron-updater"
import { AllPublishOptions } from "builder-util-runtime"
import log from './logger'
// Or MacUpdater, AppImageUpdater
export default class AppUpdater {
constructor() {
const server = 'http://localhost:8080'
const feed = `${server}/updates/${process.platform}`
const options = {
requestHeaders: {
// Any request headers to include here
},
provider: 'generic',
url: feed
} as AllPublishOptions
const autoUpdater = new NsisUpdater(options)
autoUpdater.logger = log
autoUpdater.checkForUpdatesAndNotify()
}
}

20
src/logger.ts Normal file
View File

@ -0,0 +1,20 @@
// logger.ts
import log from 'electron-log';
// 关闭控制台打印
log.transports.console.level = false;
// 设置文件日志级别
log.transports.file.level = 'debug';
// 设置文件最大大小10MB
log.transports.file.maxSize = 10 * 1024 * 1024;
// 设置日志文件路径和命名方式
log.transports.file.resolvePath = () => {
const date = new Date();
const dateStr = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
return `log/${dateStr}.log`;
};
// 导出日志方法
export default log;