blob: 0582868f1b7f0d43d3dd0fa9ba5a391c2c365e2c [file] [log] [blame]
Brandon Walderman110d5922022-04-19 10:35:07 -07001let _fs
Yang Guo4fd355c2019-09-19 10:59:03 +02002try {
3 _fs = require('graceful-fs')
4} catch (_) {
5 _fs = require('fs')
6}
Brandon Walderman110d5922022-04-19 10:35:07 -07007const universalify = require('universalify')
8const { stringify, stripBom } = require('./utils')
Yang Guo4fd355c2019-09-19 10:59:03 +02009
Brandon Walderman110d5922022-04-19 10:35:07 -070010async function _readFile (file, options = {}) {
Yang Guo4fd355c2019-09-19 10:59:03 +020011 if (typeof options === 'string') {
Brandon Walderman110d5922022-04-19 10:35:07 -070012 options = { encoding: options }
Yang Guo4fd355c2019-09-19 10:59:03 +020013 }
14
Brandon Walderman110d5922022-04-19 10:35:07 -070015 const fs = options.fs || _fs
Yang Guo4fd355c2019-09-19 10:59:03 +020016
Brandon Walderman110d5922022-04-19 10:35:07 -070017 const shouldThrow = 'throws' in options ? options.throws : true
Yang Guo4fd355c2019-09-19 10:59:03 +020018
Brandon Walderman110d5922022-04-19 10:35:07 -070019 let data = await universalify.fromCallback(fs.readFile)(file, options)
Yang Guo4fd355c2019-09-19 10:59:03 +020020
Brandon Walderman110d5922022-04-19 10:35:07 -070021 data = stripBom(data)
Yang Guo4fd355c2019-09-19 10:59:03 +020022
Brandon Walderman110d5922022-04-19 10:35:07 -070023 let obj
24 try {
25 obj = JSON.parse(data, options ? options.reviver : null)
26 } catch (err) {
27 if (shouldThrow) {
28 err.message = `${file}: ${err.message}`
29 throw err
30 } else {
31 return null
Yang Guo4fd355c2019-09-19 10:59:03 +020032 }
Brandon Walderman110d5922022-04-19 10:35:07 -070033 }
Yang Guo4fd355c2019-09-19 10:59:03 +020034
Brandon Walderman110d5922022-04-19 10:35:07 -070035 return obj
Yang Guo4fd355c2019-09-19 10:59:03 +020036}
37
Brandon Walderman110d5922022-04-19 10:35:07 -070038const readFile = universalify.fromPromise(_readFile)
39
40function readFileSync (file, options = {}) {
Yang Guo4fd355c2019-09-19 10:59:03 +020041 if (typeof options === 'string') {
Brandon Walderman110d5922022-04-19 10:35:07 -070042 options = { encoding: options }
Yang Guo4fd355c2019-09-19 10:59:03 +020043 }
44
Brandon Walderman110d5922022-04-19 10:35:07 -070045 const fs = options.fs || _fs
Yang Guo4fd355c2019-09-19 10:59:03 +020046
Brandon Walderman110d5922022-04-19 10:35:07 -070047 const shouldThrow = 'throws' in options ? options.throws : true
Yang Guo4fd355c2019-09-19 10:59:03 +020048
49 try {
Brandon Walderman110d5922022-04-19 10:35:07 -070050 let content = fs.readFileSync(file, options)
Yang Guo4fd355c2019-09-19 10:59:03 +020051 content = stripBom(content)
52 return JSON.parse(content, options.reviver)
53 } catch (err) {
54 if (shouldThrow) {
Brandon Walderman110d5922022-04-19 10:35:07 -070055 err.message = `${file}: ${err.message}`
Yang Guo4fd355c2019-09-19 10:59:03 +020056 throw err
57 } else {
58 return null
59 }
60 }
61}
62
Brandon Walderman110d5922022-04-19 10:35:07 -070063async function _writeFile (file, obj, options = {}) {
64 const fs = options.fs || _fs
Yang Guo4fd355c2019-09-19 10:59:03 +020065
Brandon Walderman110d5922022-04-19 10:35:07 -070066 const str = stringify(obj, options)
Yang Guo4fd355c2019-09-19 10:59:03 +020067
Brandon Walderman110d5922022-04-19 10:35:07 -070068 await universalify.fromCallback(fs.writeFile)(file, str, options)
Yang Guo4fd355c2019-09-19 10:59:03 +020069}
70
Brandon Walderman110d5922022-04-19 10:35:07 -070071const writeFile = universalify.fromPromise(_writeFile)
Yang Guo4fd355c2019-09-19 10:59:03 +020072
Brandon Walderman110d5922022-04-19 10:35:07 -070073function writeFileSync (file, obj, options = {}) {
74 const fs = options.fs || _fs
Yang Guo4fd355c2019-09-19 10:59:03 +020075
Brandon Walderman110d5922022-04-19 10:35:07 -070076 const str = stringify(obj, options)
Yang Guo4fd355c2019-09-19 10:59:03 +020077 // not sure if fs.writeFileSync returns anything, but just in case
78 return fs.writeFileSync(file, str, options)
79}
80
Brandon Walderman110d5922022-04-19 10:35:07 -070081const jsonfile = {
82 readFile,
83 readFileSync,
84 writeFile,
85 writeFileSync
Yang Guo4fd355c2019-09-19 10:59:03 +020086}
87
88module.exports = jsonfile