electron-updater

前提备份:

安装一下更新插件

复制代码
npm install electron-updater --save 

app | Electron (electronjs.org)更多配置参考app | Electron (electronjs.org)

自动更新 electron-updater

1、安装依赖 yarn add electron-updater

2、自定义更新的文件 handleUpdate.js

注: 打包完会生成一个latest.yml文件,把这个文件和生成的.exe文件放在服务器上,会自动检测版本

自定义更新的文件 handleUpdate.js-版本1:

复制代码
import {
	autoUpdater
} from 'electron-updater'

import {
	ipcMain,dialog
} from 'electron'
let mainWindow = null;
export function handleUpdate(window, feedUrl) {
	mainWindow = window;
	let message = {
		error: '检查更新出错',
		checking: '正在检查更新......',
		updateAva: '检测到新版本,正在下载......',
		updateNotAva: '现在使用的就是最新版本,不用更新',
	};

`autoUpdater.autoDownload = false;` //取消自动下载
	//设置更新包的地址
	autoUpdater.setFeedURL(feedUrl);
	//监听升级失败事件
	autoUpdater.on('error', function(error) {
		sendUpdateMessage({
			cmd: 'error',
			message: error
		})
	});
	//监听开始检测更新事件
	autoUpdater.on('checking-for-update', function(message) {
		sendUpdateMessage({
			cmd: 'checking-for-update',
			message: message
		})
	});
	//监听发现可用更新事件
	autoUpdater.on('update-available', function(message) {
		sendUpdateMessage({
			cmd: 'update-available',
			message: message
		})
		//新加内容
	/**	`const options = {
			type: 'info',
			buttons: ['确定', '取消'],
			title: '更新提示',
			message: '发现有新版本,是否更新?',
			cancelId: 1
		}
		dialog.showMessageBox(options).then(res => {
			if (res.response === 0) {
				sendUpdateMessage({
					cmd: 'confimUpdate',
					message: message
				})
				autoUpdater.downloadUpdate()
			} else {
				return;
			}
		})`*/




	});
	//监听没有可用更新事件
	autoUpdater.on('update-not-available', function(message) {
		sendUpdateMessage({
			cmd: 'update-not-available',
			message: message
		})
	});

	// 更新下载进度事件
	autoUpdater.on('download-progress', function(progressObj) {
		sendUpdateMessage({
			cmd: 'download-progress',
			message: progressObj
		})
	});
	//监听下载完成事件
	autoUpdater.on('update-downloaded', function(event, releaseNotes, releaseName, releaseDate, updateUrl) {
		sendUpdateMessage({
			cmd: 'update-downloaded',
			message: {
				releaseNotes,
				releaseName,
				releaseDate,
				updateUrl
			}
		})
		//退出并安装更新包
		//autoUpdater.quitAndInstall();
	});
	//新增
	ipcMain.on("quit-install", (e, arg) => {
		autoUpdater.quitAndInstall();
	})
	//接收渲染进程消息,开始检查更新
	ipcMain.on("checkForUpdate", (e, arg) => {
		//执行自动更新检查
		// sendUpdateMessage({cmd:'checkForUpdate',message:arg})
		autoUpdater.checkForUpdates();
	})
}
//给渲染进程发送消息
function sendUpdateMessage(text) {
	mainWindow.webContents.send('message', text)
}

版本2:

复制代码
 const { ipcMain } = require('electron')
 const { autoUpdater } = require("electron-updater")
 const { build } = require("../../../package.json")
 
  
 // 用户反馈立即更新
 ipcMain.on('ev-update-now', () => {
     console.log('ev-update-now::: 用户同意更新,开始更新') 
  
     autoUpdater.quitAndInstall()
 })
  
 // 用户也可以通过点击按钮去检测更新
 ipcMain.on('ev-check-for-update', () => {
     console.log('ev-check-for-update::: 执行自动更新检查') 
  
     autoUpdater.checkForUpdates()
 })
  
 function handleUpdate(mainWindow) {
     const message = {
         error: '检查更新出错',
         checking: '正在检查更新......',
         updateAva: '检测到新版本,正在下载......',
         updateNotAva: '现在使用的就是最新版本,不用更新'
     }
  
     autoUpdater.setFeedURL(build.publish[0].url) // 设置下载地址
  
     // 检查更新出错
     autoUpdater.on('error', () => {
         console.log('autoUpdater-error:::', arguments)
  
         sendUpdateMessage(message.error)
     })
  
     // 检查是否有版本更新
     autoUpdater.on('checking-for-update', () => {
         console.log('checking-for-update:::', arguments)
  
         sendUpdateMessage(message.checking)
     })
  
     // 检测到有版本更新
     autoUpdater.on('update-available', () => {
         console.log('update-available:::', arguments)
  
         sendUpdateMessage(message.updateAva)
     })
  
     // 未发现有新版本
     autoUpdater.on('update-not-available', () => {
         console.log('update-not-available:::', arguments)
  
         sendUpdateMessage(message.updateNotAva)
     })
  
     // 更新下载进度事件
     autoUpdater.on('download-progress', progressObj => {
         console.log('download-progress:::', progressObj)
         
         mainWindow.setProgressBar(progressObj.percent / 100)
     })
  
     // 下载完成,询问用户是否更新
     autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) => {
         console.log('update-downloaded::: 下载完成,询问用户是否更新') 
         
         mainWindow.webContents.send('ev-should-update', {
             event,
             releaseNotes,
             releaseName,
             releaseDate,
             updateUrl,
             quitAndUpdate
         })
     })
  
     function sendUpdateMessage(text) {
         mainWindow.webContents.send('ev-message', text)
     }
 }
  
 module.exports = {
     handleUpdate
 }

3、在主进程文件main.js里面引用 handleUpdate.js

复制代码
const {
	handleUpdate
} = require('./handleUpdate.js') //根据自己路径引入 我跟main.js同级放的    

使用:

复制代码
  mainWindow.on('closed', () => {
    mainWindow = null
  })
 
  //自动更新函数
  handleUpdate(mainWindow) // 在这里执行,并把mainWindow传入
相关推荐
万少2 分钟前
万少的 Claude Code 入门教程
前端·人工智能·后端
এ慕ོ冬℘゜12 分钟前
JS 前端基础高频面试题
开发语言·前端·javascript
放下华子我只抽RuiKe515 分钟前
React 从入门到生产(八):测试与部署
前端·javascript·深度学习·react.js·前端框架·ecmascript·集成学习
Dxy123931021618 分钟前
JS列表获取指定范围值的 N 种方法
开发语言·javascript·ecmascript
蜡笔小电芯22 分钟前
【Electron】第2章—BrowserWindow 与 Electron 窗口机制
前端·javascript·electron
zhangxingchao25 分钟前
AI 大模型面试核心二:微调、RAG、MCP、Agent 与工程落地
前端·人工智能·后端
ZC跨境爬虫26 分钟前
跟着 MDN 学CSS day_15:(掌握CSS背景与边框的创造性用法)
前端·css·ui·html·tensorflow
zhangxingchao27 分钟前
AI 大模型面试核心三: RAG、Agent 到 Prompt Engineering 的工程化理解
前端·人工智能·后端
Hilaku29 分钟前
从 15MB 减到 800KB,一行 ffmpeg 解决3D 渲染卡顿问题
前端·javascript·程序员
彦为君30 分钟前
JavaSE-11-ByteBuffer(NIO核心组件)
java·开发语言·前端·数据库·后端·spring·nio