forked from nornagon/cdda-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-fixtures.js
58 lines (55 loc) · 1.66 KB
/
fetch-fixtures.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
import * as fs from "fs/promises";
import { createWriteStream, createReadStream, readFileSync } from "fs";
import * as crypto from "crypto";
import * as url from "url";
import fetch from "node-fetch";
const { buildNum, sha } = JSON.parse(
readFileSync("./_test/all.meta.json", "utf8")
);
const update = process.argv[2] === "latest";
const dataUrl = `https://raw.githubusercontent.com/nornagon/cdda-data/main/data/${
update ? "latest" : buildNum
}/all.json`;
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
function computeSha(file) {
return new Promise((resolve) => {
const hash = crypto.createHash("sha256");
const s = createReadStream(file);
s.on("data", (d) => hash.update(d));
s.on("end", () => {
resolve(hash.digest("hex"));
});
});
}
async function matchesSha(file, sha) {
return sha === (await computeSha(file));
}
(async () => {
const filename = __dirname + "/_test/all.json";
if (
!(await fs.access(filename).then(
() => true,
() => false
))
) {
console.log("Test data not present, fetching...");
} else if (!(await matchesSha(filename, sha)) || update) {
if (update) console.log("Updating test data...");
else console.log("Test data not up-to-date, fetching...");
} else {
return;
}
const res = await fetch(dataUrl);
const dest = createWriteStream(filename);
res.body.pipe(dest);
res.body.on("end", async () => {
const sha = await computeSha(filename);
const buildNum = JSON.parse(
await fs.readFile(filename, "utf8")
).build_number;
await fs.writeFile(
__dirname + "/_test/all.meta.json",
JSON.stringify({ buildNum, sha })
);
});
})();