forked from nornagon/cdda-guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-fixtures.js
43 lines (40 loc) · 1.44 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
const fs = require('fs/promises')
const { createWriteStream, createReadStream } = require('fs')
const crypto = require('crypto')
const { buildNum, sha } = require('./_test/all.meta.json')
const update = process.argv[2] === 'latest'
const dataUrl = `https://raw.githubusercontent.com/nornagon/cdda-data/main/data/${update ? 'latest' : buildNum}/all.json`
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 require('node-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}))
});
})()