electron-vite_10electron-updater软件更新

468 阅读3分钟

网很多electron-updater更新文章,这里只简单写一下演示代码;

为什么选择

electron-updater插件可以自动更新应用程序,同时支持多个平台;比官方要强; 官方的autoUpdater仅支持macOS 和 Windows 自动更新; 注意是自动,直接更新那种;

脚手架中是含有的electron-updater的
// 安装依赖如果你没有的话
npm i electron-updater
// 引入项目
const { autoUpdater } = require('electron-updater');
// 配置项
autoUpdater.forceDevUpdateConfig = true //开发环境下强制更新
autoUpdater.autoDownload = false; // 自动下载
autoUpdater.autoInstallOnAppQuit = true; // 应用退出后自动安装
封装一个简单方法
/****************  检查版本更新 start ****************
 * autoUpdater.checkForUpdates(); 检测是否最新版
 * autoUpdater.downloadUpdate(); 下载最新版
*/
// 检查是否更新
// 是否开启了检测
let isStartCheckForUpdates = false;
let customDialog: any = null;
function checkForUpdates(mainWindow, url?: string) {
  if (isStartCheckForUpdates) {
    return;
  }
  isStartCheckForUpdates = true;
  if (typeof url !== 'string') {
    url = 'https://xxx.cn/yyhDesktopUpdate/'
  }
  // 指定更新地址
  autoUpdater.setFeedURL(url);
  autoUpdater.autoDownload = false;
  autoUpdater.checkForUpdates();
  // 开始检查更新事件; 提示语: '正在检查更新';
  autoUpdater.on('checking-for-update', function () {
  });
  // 发现可更新版本; 提示语: 检测到新版本,准备下载
  autoUpdater.on('update-available', function () {
    if (customDialog) {
      customDialog.close();
      customDialog = null;
    }
    // 系统原生弹窗让用户选择是否更新
    customDialog = dialog.showMessageBox(mainWindow, {
      type: 'info',
      title: '检测到有新版本',
      message: '检测到新版本,是否立即更新?',
      buttons: ['确定', '取消'],
      cancelId: 1, // 指定“取消”按钮的索引
    }).then((result) => {
      customDialog = null;
      if (result.response === 0) {
        // 下载最新版
        autoUpdater.downloadUpdate();
      } else { // 用户点击了“取消”
      }
      isStartCheckForUpdates = false;
    }).catch(() => {
      customDialog = null;
      isStartCheckForUpdates = false;
    });
  });
  // 没有可更新版本事件; 提示语: '已经是最新版本';
  autoUpdater.on('update-not-available', function () {
    isStartCheckForUpdates = false;
  });
  // 更新发生错误时事件; 提示语: '软件更新异常,请重试';
  autoUpdater.on('error', function () {
    if (customDialog) {
      customDialog.close();
      customDialog = null;
    }
    // 提示用户更新失败
    customDialog = dialog.showMessageBox({
      type: 'info',
      title: '更新检查',
      message: '软件更新异常,请重试!',
      buttons: ['好的'],
    }).then(() => {
      customDialog = null;
    });
  });
  // 更新下载完毕后事件; 提示语: '下载完成,准备安装';
  autoUpdater.on('update-downloaded', function () {
    if (customDialog) {
      customDialog.close();
      customDialog = null;
    }
    // 下载后安装更新
    customDialog = dialog.showMessageBox({
      type: 'info',
      title: '更新完成',
      message: '更新已下载完成,是否立即安装应用?',
      buttons: ['立即安装'],
    }).then((result) => {
      customDialog = null;
      if (result.response === 0) {
        autoUpdater.quitAndInstall();
      }
    }).catch(() => {
      customDialog = null;
    });
  });
  // 更新下载进度事件; 提示语: '软件下载中,请耐心等待';
  autoUpdater.on('download-progress', function (progressObj) {
    /** progressObj {
        total: 1000000, // 总字节数,例如 1MB
        delta: 1024, // 自上次事件以来下载的字节数,例如 1KB
        transferred: 512000, // 已下载的总字节数,例如 512KB
        percent: 0.5, // 下载进度的百分比,例如 50%
        bytesPerSecond: 2048 // 当前下载速度,例如 2KB/s
      }
    */
    // 使用原生进度条
    if (progressObj.percent !== undefined) {
      mainWindow.setProgressBar(progressObj.percent);
    } else {
      mainWindow.setProgressBar(-1);
    }
  });
}
/****************  检查版本更新 end *****************/
调用
mainWindow.on('ready-to-show', () => {
    mainWindow.webContents.closeDevTools();
    mainWindow.show()
    // 调用检测方法
    checkForUpdates(mainWindow);
  })
软件更新win下载到哪个文件夹了
C:\Users\Administrator\AppData\Local
判断当前是那个系统_如果做系统差异化说不定用得到
/* 判断当前是那个系统
  * 如果 process.platform 返回的是 'darwin',则表示当前操作系统是 macOS。
  * 如果 process.platform 返回的是 'win32',则表示当前操作系统是 Windows。
    - 如果 process.arch 返回的是 'ia32',则表示当前系统是 32 位。
    - 如果 process.arch 返回的是 'x64',则表示当前系统是 64 位。
  * 如果 process.platform 返回的是 'linux',则表示当前操作系统是 Linux。
*/