electron自动更新版本,复制可用

1、安装electron-updater要保证这3个安装在package.json - devDependencies里面,否则打包会缺少模块
2、其他报错有可能 electron版本和electron-updater 版本不兼容,兼容情况查询官网
3、setFeedURL() 服务器地址目录 例如: www.baidu.com/version/
在服务器version目录下放exe安装包和latest.yml文件,
系统自动对比latest.yml版本号不一样就会触发更新了
4、根目录添加electron-builder.yml文件

// package.json
"electron-updater": "^6.1.8",
 "electron": "^25.6.0",
 "electron-builder": "^24.6.3",
// autoUpdater.js
import { dialog, ipcMain,app } from 'electron'
import { autoUpdater } from 'electron-updater' 
import { mainWindow } from './index'
 
//自动下载更新
autoUpdater.autoDownload = false
//退出时自动安装更新
autoUpdater.autoInstallOnAppQuit = false 

export function autoUpdaterFunc() {
  // 设置更新源url 
  autoUpdater.setFeedURL('http://.......') 
  //检查是否有更新
  setTimeout(() => { 
    autoUpdater.checkForUpdates()
  }, 2000);

  //有新版本时
  autoUpdater.on('update-available', (_info) => {
    // console.log('new version',_info)
    dialog
      .showMessageBox({
        type: 'warning',
        title: '更新提示',
        message: `新版本${_info.version}已更新,是否现在下载?`,
        buttons: ['下载', '取消'],
        cancelId: 1
      })
      .then((res) => {
        if (res.response == 0) {
          //开始下载更新
          autoUpdater.downloadUpdate()
        }
      })
  })

  //没有新版本时
  autoUpdater.on('update-not-available', (_info) => {
    console.log('no update')
  })

  //更新下载完毕
  autoUpdater.on('update-downloaded', (_info) => {
    //退出并安装更新
    // autoUpdater.quitAndInstall()
    dialog.showMessageBox({
        type: 'info',
        title: '升级提示!',
        message: '安装会退出软件,是否现在安装?',
        buttons: ['安装', '取消'],
        cancelId: 1
      }).then((res) => {
        if (res.response == 0) { 
          autoUpdater.quitAndInstall()
        }
      })
  })

  //更新发生错误
  autoUpdater.on('error', (_info) => {
    console.log('update error', _info)
  })

  // 监听下载进度
  autoUpdater.on('download-progress', (progressObj) => { 
    mainWindow.webContents.send('download-progress', progressObj.percent)
  })
  
// 页面中使用方法
//window.electron.ipcRenderer.on('download-progress', (e, data) => {   
// data包含总大小,网速,已下载进度等等
//  progress.value = data //进度百分百
// })

  autoUpdater.on('before-quit-for-update', () => {
    // console.log('清楚缓存'); 
  });
}

main/index.js 中app.whenReady()加入即可

// 本地测试dev环境下触发自动更新 加在最外层即可
Object.defineProperty(app, 'isPackaged', {
  get() {
      return true;
  }
})


app.whenReady().then(() => {  
  electronApp.setAppUserModelId('com.electron') 
  app.on('browser-window-created', (_, window) => {
    optimizer.watchWindowShortcuts(window)
  }) 
  createWindow() 
 
  autoUpdaterFunc()   // 打开后2秒触发自动更新 
  
  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  }) 
})

根目录添加electron-builder.yml文件

appId: com.app名字.app
productName: app名字
directories:
  buildResources: build
files:
  - '!**/.vscode/*'
  - '!src/*'
  - '!electron.vite.config.{js,ts,mjs,cjs}'
  - '!{.eslintignore,.eslintrc.cjs,.prettierignore,.prettierrc.yaml,dev-app-update.yml,CHANGELOG.md,README.md}'
  - '!{.env,.env.*,.npmrc,pnpm-lock.yaml}'
asarUnpack:
  - resources/**
win:
  executableName: app名字
nsis:
  artifactName: ${productName}.${ext}
  shortcutName: ${productName}
  uninstallDisplayName: ${productName}
  createDesktopShortcut: always
  allowToChangeInstallationDirectory: true
  oneClick: false
mac:
  entitlementsInherit: build/entitlements.mac.plist
  extendInfo:
    - NSCameraUsageDescription: Application requests access to the device's camera.
    - NSMicrophoneUsageDescription: Application requests access to the device's microphone.
    - NSDocumentsFolderUsageDescription: Application requests access to the user's Documents folder.
    - NSDownloadsFolderUsageDescription: Application requests access to the user's Downloads folder.
  notarize: false
dmg:
  artifactName: ${productName}.${ext}
linux:
  target:
    - AppImage
    - snap
    - deb
  maintainer: electronjs.org
  category: Utility
appImage:
  artifactName: ${productName}.${ext}
npmRebuild: false
publish:
  provider: generic
  # url: http://localhost:3000/
  url: 服务器更新地址目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值