-
Notifications
You must be signed in to change notification settings - Fork 3
/
calculate.js
87 lines (78 loc) · 3.23 KB
/
calculate.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
const fs = require("fs")
const fetch = require("node-fetch").default
const crypto = require("crypto")
const sources = process.argv[2] ? [process.argv[2]] : fs.readFileSync(__dirname+"/sources.txt", "utf8").split(/[\r\n]+/g).filter(e => !!e && !e.startsWith("#"))
console.log(`Deprecated: use calculateAllCommits.js and create a gh-token file with your github token.`)
sources.forEach(src => {
fetch(src)
.then(async res => {
if(res.status !== 200)return console.error(`\x1b[31m${src} returned ${res.status}.\x1b[0m`)
const body = await res.buffer()
const hash = crypto.createHash("sha256").update(body).digest("hex")
const type = src.endsWith(".js") ? "Plugin" : "Theme"
let options
try{
options = parseMeta(body.toString("utf8"))
}catch(e){
console.log(src, e)
return
}
console.log(`\x1b[32m${src.replace("https://raw.githubusercontent.com/", "")}: \x1b[33m${hash}\x1b[0m`)
if(fs.existsSync(__dirname+"/hashes/"+hash))return
if(src.includes("Lightcord/BetterDiscordAddons")){// official
fs.writeFileSync(__dirname+"/hashes/"+hash, JSON.stringify({
type,
name: options.displayName || options.name,
official: true
}, null, " "))
}else{
fs.writeFileSync(__dirname+"/hashes/"+hash, JSON.stringify({
type,
name: options.displayName || options.name
}, null, " "))
}
}).catch(console.error)
})
/** Theses functions were taken from the BetterDiscordApp scripts. */
function parseMeta(content){
const firstLine = content.split("\n")[0];
const hasOldMeta = firstLine.includes("//META");
if (hasOldMeta) return parseOldMeta(content);
const hasNewMeta = firstLine.includes("/**");
if (hasNewMeta) return parseNewMeta(content);
throw new Error("META was not found.");
}
function parseOldMeta(content) {
const meta = content.split("\n")[0];
const rawMeta = meta.substring(meta.lastIndexOf("//META") + 6, meta.lastIndexOf("*//"));
if (meta.indexOf("META") < 0) throw new Error("META was not found.");
const parsed = JSON.parse(rawMeta);
if (!parsed) throw new Error("META could not be parsed.");
if (!parsed.name) throw new Error("META missing name data.");
parsed.format = "json";
return parsed;
}
const splitRegex = /[^\S\r\n]*?(?:\r\n|\n)[^\S\r\n]*?\*[^\S\r\n]?/;
const escapedAtRegex = /^\\@/;
function parseNewMeta(content) {
const block = content.split("/**", 2)[1].split("*/", 1)[0];
const out = {};
let field = "";
let accum = "";
for (const line of block.split(splitRegex)) {
if (line.length === 0) continue;
if (line.charAt(0) === "@" && line.charAt(1) !== " ") {
out[field] = accum;
const l = line.indexOf(" ");
field = line.substr(1, l - 1);
accum = line.substr(l + 1);
}
else {
accum += " " + line.replace("\\n", "\n").replace(escapedAtRegex, "@");
}
}
out[field] = accum.trim();
delete out[""];
out.format = "jsdoc";
return out;
}