forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-json-file.js
32 lines (29 loc) · 975 Bytes
/
read-json-file.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
import fs from 'fs'
import { brotliDecompressSync } from 'zlib'
export default function readJsonFile(xpath) {
return JSON.parse(fs.readFileSync(xpath, 'utf8'))
}
export function readCompressedJsonFile(xpath) {
if (!xpath.endsWith('.br')) {
xpath += '.br'
}
return JSON.parse(brotliDecompressSync(fs.readFileSync(xpath)))
}
// Ask it to read a `foo.json` file and it will automatically
// first see if there's a `foo.json.br` and only if it's not,
// will fallback to reading the `foo.json` file.
// The reason for this is that staging builds needs to as small as
// possible (in terms of disk) for them to deploy faster. So the
// staging deployment process will compress a bunch of large
// `.json` files before packaging it up.
export function readCompressedJsonFileFallback(xpath) {
try {
return readCompressedJsonFile(xpath)
} catch (err) {
if (err.code === 'ENOENT') {
return readJsonFile(xpath)
} else {
throw err
}
}
}