This repository has been archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.js
173 lines (157 loc) · 5.31 KB
/
backend.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const {net, app} = require("electron");
const fs = require("fs");
const path = require("path");
const admZip = require("adm-zip");
const harponica = require("@uhsse/harponica");
const rimraf = require("rimraf");
const request = require("request");
const storage = require("electron-json-storage");
class Backend {
constructor() {
this.userDataPath = app.getPath("userData");
this.repoURL = "https://github.com/UniversityHigh/universityhigh.github.io/archive/master.zip";
this.headURL = "https://api.github.com/repos/universityhigh/universityhigh.github.io/git/refs/heads/master";
this.baseCommitURL = "https://api.github.com/repos/UniversityHigh/universityhigh.github.io/git/commits";
this.repoPath = path.join(this.userDataPath, "universityhigh.github.io-master");
this.harponicaPath = path.join(this.repoPath, "_harponica");
this.localsPath = path.join(this.harponicaPath, "_locals.json");
this.globalsPath = path.join(this.harponicaPath, "_globals.json");
this.server = undefined;
this.serving = false;
}
checkForRepo() {
return fs.existsSync(this.repoPath);
}
checkForUpdates(msgCallback, errCallback) {
this.getLatestCommitHash((hash) => {
this.getHashFromDisk((hashOnDisk) => {
if (hash == hashOnDisk) return msgCallback(null);
this.getCommitMessageFromHash(hash, (msg) => {
msgCallback(msg);
}, (err) => msgCallback(`Error while obtaining latest commit message, however there are updates available: ${err})`));
});
}, (err) => errCallback(`Error while obtaining latest commit hash: ${err}`));
}
getLatestCommitHash(hashCallback, errCallback) {
request({
url: this.headURL,
headers: {
"User-Agent": "UniversityHighEditor"
}
}, (err, res, body) => {
if (err) return errCallback(err);
if (res.statusCode != 200) return errCallback(`Received error code from server: ${res.statusCode}`);
try {
let json = JSON.parse(body);
let hash = json.object.sha;
hashCallback(hash);
}
catch (err) {
errCallback(`Received invalid data from server: ${err}`);
}
});
}
getHashFromDisk(hashCallback) {
storage.get("commitOnDisk", (err, json) => {
if (err) return hashCallback(null);
hashCallback(json.sha);
});
}
getCommitMessageFromHash(hash, msgCallback, errCallback) {
request({
url: `${this.baseCommitURL}/${hash}`,
headers: {
"User-Agent": "UniversityHighEditor"
}
}, (err, res, body) => {
if (err) return errCallback(err);
if (res.statusCode != 200) return errCallback(`Received error code from server: ${res.statusCode}`);
try {
let json = JSON.parse(body);
let msg = json.message;
msgCallback(msg);
}
catch (err) {
errCallback(`Received invalid data from server: ${err}`);
}
});
}
fetchRepo(errCallback, progressCallback, successCallback) {
let request = new net.request(this.repoURL);
let currentBytes = 0;
let totalBytes = 0;
request.on("response", (res) => {
if (!("content-length" in res.headers)) {
return errCallback("No content-length");
}
totalBytes = res.headers["content-length"][0];
res.on("data", (data) => {
currentBytes += data.length;
fs.appendFileSync(`${this.repoPath}.zip`, data);
progressCallback(currentBytes, totalBytes);
});
});
request.on("error", (err) => errCallback(err));
request.on("close", () => {
if (currentBytes != 0 && currentBytes == totalBytes) {
let zip = new admZip(`${this.repoPath}.zip`);
zip.extractAllTo(this.userDataPath);
fs.unlink(`${this.repoPath}.zip`, () => {}); // No longer any need for the zip
// Empty callback because apparently not passing in a callback is deprecated (?)
this.getLatestCommitHash((hash) => {
storage.set("commitOnDisk", {
sha: hash
}, (err) => {
if (err) return errCallback(`Error while saving latest hash to disk: ${err}`);
successCallback();
});
}, (err) => errCallback(`Error while obtaining latest hash for this repo: ${err}`));
}
else {
return errCallback("Download wasn't completed fully");
}
});
request.end(); // Send it
}
deleteRepo(callback) {
rimraf(this.repoPath, {disableGlob: true}, (err) => callback(err));
}
startServer(port, callback) {
if (!this.checkForRepo()) return;
if (!this.server) this.server = new harponica(this.harponicaPath);
this.server.start(port, () => {
this.serving = true;
callback();
});
}
stopServer(callback) {
if (this.server) this.server.stop(() => {
this.serving = false;
callback();
});
}
getJSONForPage(page) {
if (page == "globals") {
return JSON.parse(fs.readFileSync(this.globalsPath, "utf8"));
}
let json = JSON.parse(fs.readFileSync(this.localsPath, "utf8"));
return page ? json[page] : json;
}
setJSONForPage(page, pageJSON, callback) {
if (page == "globals") {
fs.writeFile(this.globalsPath, JSON.stringify(pageJSON, null, 2), (err) => callback(err));
return;
}
let json = JSON.parse(fs.readFileSync(this.localsPath, "utf8"));
json[page] = pageJSON;
fs.writeFile(this.localsPath, JSON.stringify(json, null, 2), (err) => callback(err)); // beautify w/ 2 tabs
}
getFileContentsBase64(filePath) {
return fs.readFileSync(path.join(this.repoPath, filePath), "base64");
}
compile(callback) {
if (!this.server) this.server = new harponica(this.harponicaPath);
this.server.compile(this.repoPath, callback);
}
}
module.exports = Backend;