electron+vite+vue+ts项目搭建(三)-打包exe上传github,electron-updater自动检测更新

1.package.json配置

owner 仓库所有者
repo 仓库名称
private //若是私有仓库,则将私有设置为true,同时添加token,反之不需要设置
token github的私有token
releaseType 上传到github的版本类型(draft:草稿,prerelease:提前发行版,release 发行版)

 "publish": [
        {
          "provider": "github",
          "owner": "test",
          "repo": "electronfile",
          "token": "ghp_******************SDJIWDL23C@23",
          "releaseType": "release",
          "channel": "latest"
        }
      ],

2.electron-updater 获取github文件实现自动更新

2.1、electron/index.ts 新增配置

npm install electron-updater

const { autoUpdater } = require('electron-updater')
const checkUpdate = () => {
    console.log("checkUpdate")
    //检测更新
    autoUpdater.checkForUpdates()
    console.log("checkForUpdates")
    //正在检查更新。
    autoUpdater.on('checking-for-update', () => {
        console.log('Checking for update...')
    })
    //有可用的更新。
    autoUpdater.on('update-available', () => {
        console.log('found new version')
    })
    //没有可用的更新。
    autoUpdater.on('update-not-available', () => {
        console.log('Update not available')
    })
    //监听'error'事件
    autoUpdater.on('error', (err) => {
        console.log(err)

    })
    //更新包下载进度。
    autoUpdater.on('download-progress', (progressObj) => {
        console.log(`Downloaded ${progressObj.percent}%`)
    })


    //默认会自动下载新版本,如果不想自动下载,设置autoUpdater.autoDownload = false
    //监听'update-downloaded'事件,新版本下载完成时触发
    autoUpdater.on('update-downloaded', () => {
        console.log('update-downloaded')

        dialog.showMessageBox({
            type: 'info',
            title: '应用更新',
            message: '发现新版本,是否更新?',
            buttons: ['是', '否']
        }).then((buttonIndex) => {
            if(buttonIndex.response == 0) {  //选择是,则退出程序,安装新版本
                autoUpdater.quitAndInstall()
                app.quit()
            }
        })
    })

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值