-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path_build.js
90 lines (74 loc) · 2.19 KB
/
_build.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 https = require("node:https");
const fs = require("node:fs");
const yaml = require("js-yaml");
const core = require("@actions/core"); // required to be able to fail correctly
const url = "https://susbolaget.emrik.org/v1/products";
// uses https instaed of fetch to bring down the amount of dependencies
https
.get(url, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
let json;
try {
json = JSON.parse(body);
} catch (error) {
// exit and don't change the website if error is detected
core.setFailed(error.message);
}
if (!json.length || json.length < 100) {
core.setFailed("No products found, backend is probably down");
}
console.log(`Found ${json.length} products`);
for (let i = 0; i < json.length; i++) {
if (json[i].alcoholPercentage == null) {
json[i].alcoholPercentage = 0;
}
let apk =
(json[i].alcoholPercentage * json[i].volume) / json[i].price / 100;
// happens when divided by zero and such
if (apk == null) {
apk = 0;
}
// compress all tags to one
json[i].tags = [
json[i].categoryLevel1,
json[i].categoryLevel2,
json[i].categoryLevel3,
json[i].categoryLevel4,
].filter((n) => n);
if (json[i].assortmentText === "Ordervaror") {
json[i].tags.push("Ordervara");
}
if (json[i].assortmentText === "Webblanseringar") {
json[i].tags.push("Webblansering");
}
// json[i].isTemporaryOutOfStock ||
if (json[i].isCompletelyOutOfStock || json[i].isDiscontinued) {
json[i].tags.push("Slut i lager");
}
if (json[i].isNews) {
json[i].tags.push("Nyhet");
}
// round to three decimal places
json[i].apk = Math.round((apk + Number.EPSILON) * 1000) / 1000;
}
json.sort((a, b) => b.apk - a.apk);
fs.writeFile("_data/apk.yaml", yaml.dump(json), (error) => {
if (error) {
core.setFailed(error.message);
} else {
console.log("Build: Successfully wrote apk.yaml");
}
});
} catch (error) {
core.setFailed(error.message);
}
});
})
.on("error", (error) => {
core.setFailed(error.message);
});