summaryrefslogtreecommitdiff
path: root/tools/news.js
blob: 66e98ee4904627354575b44aa6c98aeb525fb1df (plain) (blame)
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
/**
 * fetches the full news file and generates a json file with the first entry
 * and another with the full archive
 */

const https = require("https");
const fs = require("fs");

const source = "https://updates.tmw2.org/legacy/news.json";

console.info("Acquiring news from static site...");

https.get(source, res => {
	let file = "";

	res.on("data", chunk => file += chunk);

	res.on("end", () => {
		const news = JSON.parse(file);
		const first = [];

		console.info(`Received ${news.length} news entries; splitting...`);

		if (news.length > 0) {
			first.push(news[0]);

			let data = new Uint8Array(Buffer.from(JSON.stringify(first)));
			fs.writeFileSync("src/assets/news.json", data, {
				encoding: "utf-8",
				flag: "w+",
			});

			data = new Uint8Array(Buffer.from(file));
			fs.writeFileSync("src/assets/news-full.json", data, {
				encoding: "utf-8",
				flag: "w+",
			});
		} else {
			console.info("Creating dummy news files...");

			let data = new Uint8Array(Buffer.from("[]"));
			fs.writeFileSync("src/assets/news.json", data, {
				encoding: "utf-8",
				flag: "w+",
			});

			fs.writeFileSync("src/assets/news-full.json", data, {
				encoding: "utf-8",
				flag: "w+",
			});
		}

		console.info("Success.");
	});

});