1 | var fs = require('fs');
|
2 | var path = require('path');
|
3 | var flatted = require('flatted');
|
4 |
|
5 | module.exports = {
|
6 | tryParse: function (filePath, defaultValue) {
|
7 | var result;
|
8 | try {
|
9 | result = this.readJSON(filePath);
|
10 | } catch (ex) {
|
11 | result = defaultValue;
|
12 | }
|
13 | return result;
|
14 | },
|
15 |
|
16 | |
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | readJSON: function (filePath) {
|
24 | return flatted.parse(
|
25 | fs.readFileSync(filePath, {
|
26 | encoding: 'utf8',
|
27 | })
|
28 | );
|
29 | },
|
30 |
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | writeJSON: function (filePath, data) {
|
39 | fs.mkdirSync(path.dirname(filePath), {
|
40 | recursive: true,
|
41 | });
|
42 | fs.writeFileSync(filePath, flatted.stringify(data));
|
43 | },
|
44 | };
|