試用了下,electron 自帶的熱更新 並非特別理想。javascript
想本身處理下載更新文件。恰好看到了網上有一個比較好的處理方式。試了下效果還能夠。java
使用如下命令將此庫包含在項目中:ajax
npm install request
在腳本的頂部聲明基本依賴項。shell
var request = require('request'); var fs = require('fs');
將GET數據流式傳輸到文件輸出。 npm
function downloadFile(file_url , targetPath){ // Save variable to know progress var received_bytes = 0; var total_bytes = 0; var req = request({ method: 'GET', uri: file_url }); var out = fs.createWriteStream(targetPath); req.pipe(out); req.on('response', function ( data ) { // Change the total bytes value to get progress later. total_bytes = parseInt(data.headers['content-length' ]); }); req.on('data', function(chunk) { // Update the received bytes received_bytes += chunk.length; showProgress(received_bytes, total_bytes); }); req.on('end', function() { alert("File succesfully downloaded"); }); } function showProgress(received,total){ var percentage = (received * 100) / total; console.log(percentage + "% | " + received + " bytes out of " + total + " bytes."); }
使用方式:json
downloadFile("http://www.planwallpaper.com/static/images/butterfly-wallpaper.jpeg", "./butterfly-wallpaper.jpeg");
用promise實現的方式:promise
/** * Promise based download file method */ function downloadFile(configuration){ return new Promise(function(resolve, reject){ // Save variable to know progress var received_bytes = 0; var total_bytes = 0; var req = request({ method: 'GET', uri: configuration.remoteFile }); var out = fs.createWriteStream(configuration.localFile); req.pipe(out); req.on('response', function ( data ) { // Change the total bytes value to get progress later. total_bytes = parseInt(data.headers['content-length' ]); }); // Get progress if callback exists if(configuration.hasOwnProperty("onProgress")){ req.on('data', function(chunk) { // Update the received bytes received_bytes += chunk.length; configuration.onProgress(received_bytes, total_bytes); }); }else{ req.on('data', function(chunk) { // Update the received bytes received_bytes += chunk.length; }); } req.on('end', function() { resolve(); }); }); }
使用方式:app
downloadFile({ remoteFile: "http://www.planwallpaper.com/static/images/butterfly-wallpaper.jpeg", localFile: "/var/www/downloads/butterfly-wallpaper.jpeg", onProgress: function (received,total){ var percentage = (received * 100) / total; console.log(percentage + "% | " + received + " bytes out of " + total + " bytes."); } }).then(function(){ alert("File succesfully downloaded"); });
還有一步 是對zip 包進行解壓 而後 替換本地文件,刪除這個更新的壓縮包 就完成了熱更新。electron
我是再渲染線程 進行判斷版本的:ui
判斷是否須要強制更新,若是不強制更新的版本都用熱更新 去下載 替換本地文件就能夠了
function UpdateApp() { $.ajax({ type: "GET", url: baseDevUrl + CLIENTUPDATES + '?OS=' + getPlatformName(), headers: { Authorization: 'Bearer ' + getAccessToken(), Accept: "application/json; charset=utf-8", }, success: function (res) { console.log(res.data[0]) if (res.data[0]&&res.data[0].version != version) { console.log('進行強制更新'); $(".myDialog-box").show(); $(".myDialog-box").click((event)=>{ return event.preventDefault(); }) $(".headerEndDiv").addClass('disClick'); $('#btn-dialogBox').dialogBox({ hasClose: true, hasBtn: true, confirmValue: '去更新', confirm: function () { console.log('this is callback function'); shell.openExternal(res.data[0].download_url); ipcRenderer.send('window-close'); }, cancelValue: '取消', cancel: function () { toast("請更新到最新版本.."); return true; }, title: res.data[0].title, content: res.data[0].content }); } }, error: function (msg) { var rss = $(msg.responseText); console.log(rss) } }); }