1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| import $ from 'jquery'; import semver from 'semver'; import request from 'request'; import progress from 'request-progress';
const fs = window.fs; const cp = window.cp; const electron = window.electron; const {app, shell} = electron.remote;
state = { check: true, latest: {}, update: 'wait', downloadState: {} };
$.ajax({ url: appConfig.updateCheckURL, timeout: 10000, type: 'GET', cache:false, success: function (data) { let latest = data[0]; if(semver.satisfies(latest.version, '>' + app.getVersion())){ if (latest.force) { that.updateVersion(); } } }, complete: function (XMLHttpRequest, status) { that.setState({ check: false }) } });
updateVersion(){ let that = this; const platform = osInfo.platform(); try { const downloadUrl = platform === 'darwin' ? this.state.latest.download.dmg : platform === 'win32' ? this.state.latest.download.winSetup : ''; if (downloadUrl === '') return;
const downloadUrlArr = downloadUrl.split("/");
const filename = downloadUrlArr[downloadUrlArr.length-1];
const savePath = osInfo.tmpdir() + '/' + filename;
const _request = request(downloadUrl); progress(_request, { }) .on('progress', function (state) { that.setState({downloadState: state}) }) .on('error', function (err) { that.setState({ downloadState:{ error: true } }) }) .on('end', function () { if (that.state.update === 'error') return; that.setState({ update: 'install', });
setTimeout(function () { if (platform === 'darwin'){ const appName = pjson.build.productName; const appVersion = app.getVersion(); console.info(appName,appVersion); cp.execSync(`hdiutil attach '${savePath}' -nobrowse`, { stdio: ['ignore', 'ignore', 'ignore'] });
cp.execSync(`rm -rf '/Applications/${appName}.app' && cp -R '/Volumes/${appName} ${appVersion}/${appName}.app' '/Applications/${appName}.app'`);
cp.execSync(`hdiutil eject '/Volumes/${appName} ${appVersion}'`, { stdio: ['ignore', 'ignore', 'ignore'] });
app.relaunch(); app.quit(); }
if (platform === 'win32') { shell.openItem(savePath); setTimeout(function () { app.quit(); },1500) } },2000) }) .pipe(fs.createWriteStream(savePath));
that.setState({update:'download'}); } catch (e) { console.info(e); that.setState({ update: 'error', }); } }
|