-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-html.js
109 lines (100 loc) · 2.95 KB
/
build-html.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
var fs = require("fs");
var ejs = require("ejs");
var path = require("path");
var https = require("https");
var httpGetLatest = function (callback) {
const url =
"https://api.github.com/repos/rebaslight/rebaslight/releases/latest";
https
.get(url, { headers: { "User-Agent": "request" } }, (res) => {
if (res.statusCode !== 200) {
return callback(
new Error("Got a statusCode=" + res.statusCode + " for: " + url)
);
}
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
let json = JSON.parse(body);
callback(null, json);
} catch (error) {
callback(error);
}
});
})
.on("error", (error) => {
callback(error);
});
};
var btn_src = "";
btn_src += "<p>";
btn_src += "<a href=\"<%= url %>\" class=\"btn btn-primary\" onClick=\"ga('send','event','goal','click','download',1)\">";
btn_src += "<i class=\"fa fa-download\"></i> Download <%= label %>";
btn_src += "</a>";
btn_src += "</p>";
var btnTemplate = ejs.compile(btn_src);
httpGetLatest(function(err, data){
if(err) throw err;
var info = {
version: data.tag_name,
btns_by_os: {
win: [],
mac: [],
linux: [],
}
};
data.assets.forEach(function(a){
var url = a.browser_download_url;
var push = function(os, label){
info.btns_by_os[os].push({
label: label,
url: url
});
};
if(/\.exe$/i.test(url)){
return push("win", ".exe");
}
if(/\.dmg$/i.test(url)){
return push("mac", ".dmg");
}
if(/amd64\.deb$/i.test(url)){
return push("linux", ".deb (64)");
}
if(/i386\.deb$/i.test(url)){
return push("linux", ".deb (32)");
}
if(/ia32\.tar\.bz2$/i.test(url)){
return push("linux", ".tar.bz2 (32)");
}
if(/\.tar\.bz2$/i.test(url)){
return push("linux", ".tar.bz2 (64)");
}
});
var linuxDLSortScore = function(a){
var score = 0;
if(/tar/.test(a.label)){
score += 20;
}else if(/deb/.test(a.label)){
score += 10;
}
if(/64/.test(a.label)){
score += 1;
}
return score;
};
info.btns_by_os["linux"].sort(function(a, b){
return linuxDLSortScore(b) - linuxDLSortScore(a);
});
Object.keys(info.btns_by_os).forEach(function(os){
info.btns_by_os[os] = info.btns_by_os[os].map(btnTemplate);
});
var src = fs.readFileSync(path.resolve(__dirname, "./index.ejs"), "utf8");
var html = ejs.render(src, info, {});
fs.writeFileSync(path.resolve(__dirname, "./index.html"), html);
fs.writeFileSync(path.resolve(__dirname, "./latest.json"), JSON.stringify({
version: info.version.replace(/^v/, "").trim()
}));
});