2016-10-04 160 views
-1

任何人都可以從頭描述如何使用電子自動更新程序。如何使用正確的軟件包並編寫正確的CLI以獲得在Windows中具有自動更新的簡單應用程序。電子自動更新Windows

非常感謝。

回答

0

我試過electron-basic-updater, electron-asar-updater, electron-installer-windows et al。並花費數小時嘗試如何發佈/更新我的應用程序,然後用electron-packager進行包裝,並使用Squirrel進行自動更新。他們有他們的優勢。

我假設讀者具有使用Electron應用程序的基本知識,因此我不會介入基礎知識。

重要說明:您必須在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 [email protected] */ 

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; 
}); 

其他注意事項: 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安裝電子builer -save-dev的

NPM安裝電子更新 - 保存

爲了演示目的,GET HTTP服務器作爲您的Web主機服務器。

NPM安裝HTTP服務器 - 保存

構建包是很簡單的,創建兩個文件夾「構建」和「測距」,然後在 腳本的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運行DIST

自動更新,創建一個文件夾的wwwroot並假設是你的web主機服務器,並啓動你的網站爲:

NPM運行WB

副本從DIST文件夾verything到wwwroot文件夾。

好的。

詳情請參閱here