1 | import {readFileSync, promises as fs} from 'node:fs';
|
2 |
|
3 | const {readFile} = fs;
|
4 |
|
5 | const parse = (buffer, {beforeParse, reviver} = {}) => {
|
6 |
|
7 | let data = new TextDecoder().decode(buffer);
|
8 |
|
9 | if (typeof beforeParse === 'function') {
|
10 | data = beforeParse(data);
|
11 | }
|
12 |
|
13 | return JSON.parse(data, reviver);
|
14 | };
|
15 |
|
16 | export async function loadJsonFile(filePath, options) {
|
17 | const buffer = await readFile(filePath);
|
18 | return parse(buffer, options);
|
19 | }
|
20 |
|
21 | export function loadJsonFileSync(filePath, options) {
|
22 | const buffer = readFileSync(filePath);
|
23 | return parse(buffer, options);
|
24 | }
|