4

Electron应用如何在线回退版本[Downgrade]

 3 years ago
source link: https://segmentfault.com/a/1190000040005504
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在使用electron的自动更新的时候,有时候出现BUG时想回退版本时却只能向上升级,十分之无奈。
因为看官方文档和百度搜索(我的搜索技巧比较水)都是在教人如何更新升级,却没有人讲降级,所以由我带领各位了解下如何回退版本! (如何在线更新请自行百度)

如何Downgrade

不废话先上结果

autoUpdater.allowDowngrade = true

嗯这样子就结束了
看源码的过程中我发现关于检测更新的函数里有一个叫 allowDowngrade

aync isUpdateAvailable(){
// ....

  const isLatestVersionNewer = (0, _semver().gt)(latestVersion, currentVersion);
  const isLatestVersionOlder = (0, _semver().lt)(latestVersion, currentVersion);

  if (isLatestVersionNewer) {
    return true;
  }

  return this.allowDowngrade && isLatestVersionOlder;
}

虽说翻译过来意思已经很明确,但眼见为实,然后找到这个属性的解释
image.png
嗯应该是我想要的结果了,但还是不确定能不能成功
所以我设置了 autoUpdater的allowDowngrade位true 并且上测试服务器上放置了一个 1.3.9
本地为1.3.10 成功回退!
我的配置如下

// 在autoUpdater有一个属性是allowDowngrade是用来判断是否可以回退的属性
autoUpdater.allowDowngrade = true

autoUpdater.setFeedURL(downloadUrl);
autoUpdater.on('error', function (error) {
    console.log('err', error);
    sendUpdateMessage(message.error);
});

autoUpdater.on('checking-for-update', function (info) {
    console.log('checking-for-update', JSON.stringify(info));
    sendUpdateMessage(message.checking);
});
autoUpdater.on('update-available', function (info) {
    console.log('update-available', JSON.stringify(info));
    sendUpdateMessage(message.updateAva);
});
autoUpdater.on('update-not-available', function (info) {
    console.log('update-not-available', JSON.stringify(info));
    // sendUpdateMessage(message.updateNotAva);
});
// 更新下载进度事件
autoUpdater.on('download-progress', function (progressObj) {
    mainWindow.webContents.send('downloadProgress', progressObj);
});
autoUpdater.on('update-downloaded', function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
    ipcMain.on('isUpdateNow', (e, arg) => {
        // 退出并安装
        autoUpdater.quitAndInstall();
    });
    mainWindow.webContents.send('isUpdateNow');
});


ipcMain.on('checkForUpdate', () => {
    // 检查是否需要更新
    autoUpdater.checkForUpdates();
});

希望能帮助到大家!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK