-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathautoUpdater.js
90 lines (82 loc) · 3.59 KB
/
autoUpdater.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
84
85
86
87
88
89
90
const config = require("./config");
const request = require("request");
const fs = require("fs-extra");
const path = require("path");
const unzipper = require("unzipper");
class AutoUpdater {
constructor(currentVersion) {
this.currentVersion = currentVersion;
this.updateFileName = "PasteServer-update.zip";
}
checkForUpdates(dev) {
console.log(`Checking for ${dev ? "dev-" : ""}updates...`);
return new Promise(resolve => {
request(dev ? config.autoUpdate.devPackageJsonURL : config.autoUpdate.packageJsonURL, {json: true}, (error, response, body) => {
if (error) {
console.error("Error while checking for updates.", error);
resolve(false);
return;
}
const newestVersion = body.version;
if (newestVersion !== this.currentVersion) {
console.log(`There's a newer ${dev ? "dev-" : ""}version of the PasteServer available (${newestVersion})!`);
console.log(`Execute 'installUpdate${dev ? " -dev" : ""}' to download it!`);
resolve(true);
} else {
console.log("You are up to date!");
resolve(false);
}
});
});
}
downloadUpdate(dev) {
console.log(`Downloading ${dev ? "dev-" : ""}update...`);
return new Promise(resolve => {
if (!fs.existsSync(".update"))
fs.mkdirSync(".update");
request(dev ? config.autoUpdate.devZipUrl : config.autoUpdate.zipURL).on("error", error => {
console.error("Error while downloading update.", error);
resolve(false);
}).pipe(fs.createWriteStream(path.resolve(".update", this.updateFileName))).on("close", () => {
console.log("Successfully downloaded update!");
resolve(true);
});
});
}
installUpdate() {
console.log("Installing update...");
let contentFolderName = "";
return new Promise(resolve => {
fs.createReadStream(path.resolve(".update", this.updateFileName))
.pipe(unzipper.Parse()).on("entry", entry => {
const isDir = entry.type === "Directory";
if (!contentFolderName && isDir)
contentFolderName = entry.path;
const fileName = entry.path.replace(contentFolderName, "");
if (fileName && !config.autoUpdate.keepFiles.includes(fileName)) {
const filePath = path.resolve(fileName);
if (!fs.existsSync(filePath)) {
if (isDir)
fs.mkdirSync(filePath);
else
fs.writeFileSync(filePath, "");
}
if (!isDir) {
console.log(`Replacing ${fileName}.`);
entry.pipe(fs.createWriteStream(filePath));
}
} else
entry.autodrain();
}).on("error", error => {
console.error("Error while installing update.", error);
resolve();
}).on("close", () => {
console.log("Successfully installed update!");
console.log("Stopping the PasteServer for the update to be usable...");
process.exit();
});
fs.removeSync(".update");
});
}
}
module.exports = new AutoUpdater(require("./package.json").version);