forked from ClearURLs/Rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
84 lines (65 loc) · 2.07 KB
/
build.ts
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
// Script to minimize rules JSON for publishing.
// Docs: https://docs.clearurls.xyz/1.26.1/specs/rules/#dataminjson-catalog
import JSON5 from "json5";
type Provider = {
urlPattern: string;
completeProvider?: boolean;
rules?: string[];
referralMarketing?: string[];
exceptions?: string[];
rawRules?: string[];
redirections?: string[];
forceRedirection?: boolean;
};
const rule_file = "rules.jsonc";
const minimized_file = "build/rules.min.json";
const blob = await Bun.file(rule_file).text();
const json = JSON5.parse(blob);
console.log("Loaded", Object.keys(json.providers).length, "rules.");
const providers: { [key: string]: object } = {};
for (const key in json.providers) {
const p = json.providers[key];
if (!p.urlPattern || p.urlPattern === "") {
console.error(
"Missing or empty urlPattern property for provider:",
key,
);
process.abort();
}
const pm: Provider = {
urlPattern: p.urlPattern,
};
// Only set boolean values if `true`.
if (p.completeProvider === true) {
pm.completeProvider = true;
}
if (p.forceRedirection === true) {
pm.forceRedirection = true;
}
// Only keep non-empty arrays.
if (p.rules && p.rules.length > 0) {
pm.rules = p.rules;
}
if (p.referralMarketing && p.referralMarketing.length > 0) {
pm.referralMarketing = p.referralMarketing;
}
if (p.exceptions && p.exceptions.length > 0) {
pm.exceptions = p.exceptions;
}
if (p.rawRules && p.rawRules.length > 0) {
pm.rawRules = p.rawRules;
}
if (p.redirections && p.redirections.length > 0) {
pm.redirections = p.redirections;
}
providers[key] = pm;
}
const buf = JSON.stringify({ providers });
const size = await Bun.write(minimized_file, buf);
console.log("Written", size, "bytes.");
// Calculate checksum.
const hasher = new Bun.CryptoHasher("sha256");
hasher.update(buf);
const sum = hasher.digest("hex");
console.log("Checksum:", sum);
await Bun.write(`${minimized_file}.hash`, sum);