-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
48 lines (38 loc) · 1.28 KB
/
preload.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
/*
Preloads Firebase tags into /src/assets/tags.json for loading initial filters faster. Use "npm run preload" instead of calling direcly.
Usage:
- `mkdir -p /tmp/atw && firebase database:get /tags > /tmp/atw/tags.json && node ./preload.js`
Known Issues
- cannot call firebase database:get in child_process due to truncated stdout (See: https://github.com/sindresorhus/execa/issues/130)
NOT USED. Will consider if the load time is noticeable.
*/
const fs = require('fs')
const _ = require('lodash')
let tagsFile
try {
tagsFile = fs.readFileSync('/tmp/atw/tags.json')
} catch (e) {
if (e.code === 'ENOENT') {
console.error(
'ERROR: Missing /tmp/atw/tags.json. Run "npm run preload" to download collections from Firebase Realtime Database.',
)
process.exit(1)
}
throw e
}
const tagsFull = JSON.parse(tagsFile)
// strip timestamps to reduce payload
const tags = Object.entries(tagsFull).reduce(
(accum, [categoryKey, categoryValue]) => ({
...accum,
[categoryKey]: Object.entries(categoryValue).reduce(
(accumInner, [tagKey, tagValue]) => ({
...accumInner,
[tagKey]: _.pick(tagValue, ['id', 'showOnFront', 'sortOrder', 'tag']),
}),
{},
),
}),
{},
)
fs.writeFileSync('src/assets/tags.json', JSON.stringify(tags, null, 2))