首页 文章

电子自动更新Windows

提问于
浏览
-1

任何人都可以帮助我从头开始描述如何在电子中使用自动更新程序 . 如何使用正确的软件包并编写正确的CLI以获得在Windows中具有自动更新功能的简单应用程序 .

非常感谢 .

2 回答

  • 0

    我试过 electron-basic-updater, electron-asar-updater, electron-installer-windows 等 . 并且花了好几个小时尝试如何发布/更新我的应用程序,然后使用 electron-packager 进行打包,使用 Squirrel 进行自动更新 . 他们有自己的优势 .

    我假设读者具有使用Electron应用程序的基本知识,因此我没有进入基础知识 .

    Important Note: 您必须在Windows中创建一个软件包/安装程序并安装应用程序以使自动更新程序正常工作!

    在主app.js中,添加一个IPC来处理更新场景:

    ipcMain.on('check-for-update', function(event, arg) {
    
    /* AUTO UPDATER */
    const autoUpdater = electron.autoUpdater;
    const os = require('os');
    const {dialog} = require('electron');
    
    /* For Windows, PATH to DIRECTORY that has nupkg and RELEASES files (Windows alone) */
    /* And add "Options Indexes" to htaccess if you want listing on that dir --@thinkdj */
    
    var releaseDIR = config.webURL + '/releases/win' + (os.arch() === 'x64' ? '64' : '32');
    
    autoUpdater.setFeedURL(releaseDIR);
    
    autoUpdater
        .on('error', function(error){
            loggit(error);
            return dialog.showMessageBox(mainWindow, {
                type: 'info',
                icon: basePath + "/assets/refico32.ico",
                buttons: ['Dang!'],
                title: appName + ": Update Error",
                message: "Something's not right out there. Please try again later.",
                detail: "Umm... \nIt's not you, it's the server"
            });
        })
        .on('checking-for-update', function(e) {
            loggit('Checking for update at ' + releaseDIR);
        })
        .on('update-available', function(e) {
    
            var downloadConfirmation = dialog.showMessageBox(mainWindow, {
                type: 'info',
                icon: basePath + "/assets/refico32.ico",
                buttons: ['Proceed'],
                title: appName + ": Update Available",
                message: 'An update is available. The update will be downloaded in the background.',
                detail: "Size: ~42 MB"
            });
    
            loggit('Downloading update');
    
            if (downloadConfirmation === 0) {
                return;
            }
    
        })
        .on('update-not-available', function(e) {
            loggit('Update not available');
            return dialog.showMessageBox(mainWindow, {
                type: 'info',
                icon: basePath + "/assets/refico32.ico",
                buttons: ['Cool'],
                title: appName + ": No update available",
                message: "It seems you're running the latest and greatest version",
                detail: "Woot, woot! \nTalk about being tech-savvy"
            });
        })
        .on('update-downloaded',  function (event, releaseNotes, releaseName, releaseDate, updateUrl, quitAndUpdate) {
    
            var index = dialog.showMessageBox(mainWindow, {
                type: 'info',
                icon: basePath + "/assets/refico32.ico",
                buttons: ['Install Update','Later'],
                title: appName + ": Latest version downloaded",
                message: 'Please restart the app to apply the update',
                detail: releaseName + "\n\n" + releaseNotes
            });
    
            if (index === 1) return;
    
            force_quit = true;
            autoUpdater.quitAndInstall();
        });
    
    
    autoUpdater.checkForUpdates();
    
    event.returnValue = "Checking for updates: " + releaseDIR + " Install Path: " + appPath;
     });
    

    Additional Notes: 1]你的app.js必须在一开始就处理松鼠事件 . 您可以为handleSquirrelEvent编写自己的处理程序,或者只是简单的 if (require('electron-squirrel-startup')) return; . 2]在编写本文时,一旦启动了自动更新过程,用户就无法取消更新过程 .

    要创建安装程序,您的Gruntfile.js(在 npm install grunt, npm install grunt-cli 之后)应该是类似的

    module.exports = function(grunt) {
        grunt.loadNpmTasks('grunt-electron-installer');
        grunt.initConfig({
            'create-windows-installer': {
            'ia32': {
                appDirectory: "C:\\refreshie\\app\\build\\Refreshie-win32-ia32",
                outputDirectory: "C:\\refreshie\\app\\build\\Distro\\Refreshie-Win-ia32",
                loadingGif: "C:\\refreshie\\app\\assets\\images\\install.splash.gif",
                iconUrl: "C:\\refreshie\\app\\assets\\refico.ico",
                setupIcon: "C:\\refreshie\\app\\assets\\refico.ico",
                signWithParams: "/a /f C:\\refreshie\\app\\build\\tools\\cert.signingkey.pfx /p F5",
                noMsi: true
            }
        }
        });
        grunt.registerTask('default', ['create-windows-installer']);
    };
    
  • 0

    目前,进行电子自动更新的最佳方法是使用电子生成器 .

    npm install electron-builer -save-dev npm install electron-updater -save

    出于演示目的,获取http-server作为Web主机服务器 .

    npm安装http-server -save

    构建包非常简单,创建两个文件夹“build”和“dist”,然后在package.json脚本中添加它并运行

    "scripts": {
       "start": "set NODE_ENV=dev&& tsc && concurrently \"npm run tsc:w\" \"electron .\" ",
       "tsc": "tsc",
       "tsc:w": "tsc -w",
         ;
       "dist": "build -w --x64",
       "wb": "http-server wwwroot/ -p 8080",
          ;
     },
    

    npm run dist

    对于自动更新程序,创建一个文件夹wwwroot并假设它是您的Web主机服务器并启动您的网站:

    npm run wb

    从dist文件夹复制到wwwroot文件夹 .

    好的,那样做 .

    有关完整的详细信息,请参阅here

相关问题