.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / flat-cache / src / utils.js
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    * Read json file synchronously using flatted
18    *
19    * @method readJSON
20    * @param  {String} filePath Json filepath
21    * @returns {*} parse result
22    */
23   readJSON: function (filePath) {
24     return flatted.parse(
25       fs.readFileSync(filePath, {
26         encoding: 'utf8',
27       })
28     );
29   },
30
31   /**
32    * Write json file synchronously using circular-json
33    *
34    * @method writeJSON
35    * @param  {String} filePath Json filepath
36    * @param  {*} data Object to serialize
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 };