app自动更新逻辑
This commit is contained in:
parent
f4a93680cf
commit
d14162bf74
@ -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": {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -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">
|
||||||
|
@ -1,5 +1,17 @@
|
|||||||
// electron 主进程文件
|
// electron 主进程文件
|
||||||
import { app, BrowserWindow } from 'electron'
|
import { app, BrowserWindow } from 'electron'
|
||||||
|
import log from './logger'
|
||||||
|
import AppUpdater from './electron_updater'
|
||||||
|
|
||||||
|
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(() => {
|
app.whenReady().then(() => {
|
||||||
const win = new BrowserWindow({
|
const win = new BrowserWindow({
|
||||||
@ -19,8 +31,10 @@ app.whenReady().then(()=>{
|
|||||||
}
|
}
|
||||||
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
23
src/electron_updater.ts
Normal 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
20
src/logger.ts
Normal 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;
|
Loading…
Reference in New Issue
Block a user