This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupdate.js
83 lines (74 loc) · 1.89 KB
/
update.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
74
75
76
77
78
79
80
81
82
83
/**
* electron-hot-updater
* https://github.com/shangzhenyang/electron-hot-updater
*
* @author Shangzhen Yang
*/
// TODO: You need to replace the URL here with your own URL.
const REMOTE_ROOT = process.env.REMOTE_ROOT ||
"https://raw.githubusercontent.com/shangzhenyang/electron-hot-updater/master/";
try {
const fs = require("fs");
const path = require("path");
const content = document.getElementById("content");
const timestamp = "?time=" + new Date().getTime();
let files;
let progress = 0;
window.addEventListener("load", () => {
// Get the file list.
fetch(REMOTE_ROOT + "files.json" + timestamp)
.then((response) => {
if (response.ok) {
return response.json();
} else {
content.innerText += " Error (" + response.status +
")\nFailed to update.";
}
})
.then((data) => {
if (data) {
files = data;
content.innerText += " Done";
// Start updating.
next();
}
});
})
function next() {
if (progress < files.length) {
// If there is a file that is not downloaded, continue to download.
update(files[progress]);
} else {
content.innerText += "\nUpdated successfully.";
location.href = "index.html";
}
}
function update(file) {
content.innerText += "\nDownloading " + file + " . . .";
fetch(REMOTE_ROOT + file + timestamp)
.then((response) => {
if (response.ok) {
return response.text();
} else {
throw new Error(response.status);
}
})
.then((data) => {
if (data) {
// Overwrite the local file.
fs.writeFileSync(path.join(__dirname, file), data);
content.innerText += " Done";
progress += 1;
// Continue.
next();
}
})
.catch((err) => {
content.innerText += " Error (" + err.message +
")\nFailed to update.";
});
}
} catch (err) {
document.getElementById("content").innerText += " Error\n" + err.message +
"\nFailed to update.";
}