This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathupdater.js
73 lines (62 loc) · 2.1 KB
/
updater.js
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
const electron = require('electron');
const fs = require('fs');
const { default: fetch } = require('node-fetch');
const open = require('open');
const Path = require('path');
const request = require('request');
// The current version of your app.
const APP_VERSION = require('./package.json').version;
const APP_NAME = require('./package.json').name;
let UPDATE_URL = `https://api.projectmegapack.com/v1/get/update`;
async function checkForUpdates(window) {
let json;
const verifyClient = require('./src/helpers/verify');
await verifyClient.verifyClient(async function (verified) {
if (!verified) UPDATE_URL = 'https://msfs-liverypack-api.mrproper.dev/v1/get/update';
try {
json = await (await fetch(`${UPDATE_URL}/${APP_VERSION}`)).json();
} catch {
// broken api??
return;
}
if (json.data.update) {
// Update is available!
window.loadURL(`file://${__dirname}/update-available.html?newVersion=${json.data.info.latest || ''}¤tVersion=${APP_VERSION || ''}`);
const updateDir = Path.join(electron.app.getPath('temp'), APP_NAME, `/updates`);
const exePath = Path.join(updateDir, `/setup_${json.data.info.latest}.exe`);
if (fs.existsSync(exePath)) {
await fs.promises.unlink(exePath);
}
if (!fs.existsSync(updateDir)) {
await fs.promises.mkdir(updateDir, { recursive: true });
}
// let progress = null;
// ipcMain.on('get-progress', e => {
// e.reply('download-progress', progress);
// });
await new Promise((resolve, reject) => {
// reqProgress(
request(json.data.info.downloadUrl)
// ,
// { throttle: 100 }
// )
.pipe(fs.createWriteStream(exePath))
// .on('progress', p => {
// progress = p;
// })
.on('error', async err => {
console.log(`Error`);
console.error(err);
reject(err);
})
.on('finish', async () => {
open(exePath);
resolve();
});
});
}
});
}
module.exports = {
checkForUpdates,
};