UNPKG

3.2 kBJavaScriptView Raw
1var _fs
2try {
3 _fs = require('graceful-fs')
4} catch (_) {
5 _fs = require('fs')
6}
7
8function readFile (file, options, callback) {
9 if (callback == null) {
10 callback = options
11 options = {}
12 }
13
14 if (typeof options === 'string') {
15 options = {encoding: options}
16 }
17
18 options = options || {}
19 var fs = options.fs || _fs
20
21 var shouldThrow = true
22 // DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead
23 if ('passParsingErrors' in options) {
24 shouldThrow = options.passParsingErrors
25 } else if ('throws' in options) {
26 shouldThrow = options.throws
27 }
28
29 fs.readFile(file, options, function (err, data) {
30 if (err) return callback(err)
31
32 data = stripBom(data)
33
34 var obj
35 try {
36 obj = JSON.parse(data, options ? options.reviver : null)
37 } catch (err2) {
38 if (shouldThrow) {
39 err2.message = file + ': ' + err2.message
40 return callback(err2)
41 } else {
42 return callback(null, null)
43 }
44 }
45
46 callback(null, obj)
47 })
48}
49
50function readFileSync (file, options) {
51 options = options || {}
52 if (typeof options === 'string') {
53 options = {encoding: options}
54 }
55
56 var fs = options.fs || _fs
57
58 var shouldThrow = true
59 // DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead
60 if ('passParsingErrors' in options) {
61 shouldThrow = options.passParsingErrors
62 } else if ('throws' in options) {
63 shouldThrow = options.throws
64 }
65
66 try {
67 var content = fs.readFileSync(file, options)
68 content = stripBom(content)
69 return JSON.parse(content, options.reviver)
70 } catch (err) {
71 if (shouldThrow) {
72 err.message = file + ': ' + err.message
73 throw err
74 } else {
75 return null
76 }
77 }
78}
79
80function writeFile (file, obj, options, callback) {
81 if (callback == null) {
82 callback = options
83 options = {}
84 }
85 options = options || {}
86 var fs = options.fs || _fs
87
88 var spaces = typeof options === 'object' && options !== null
89 ? 'spaces' in options
90 ? options.spaces : this.spaces
91 : this.spaces
92
93 var str = ''
94 try {
95 str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n'
96 } catch (err) {
97 // Need to return whether a callback was passed or not
98 if (callback) callback(err, null)
99 return
100 }
101
102 fs.writeFile(file, str, options, callback)
103}
104
105function writeFileSync (file, obj, options) {
106 options = options || {}
107 var fs = options.fs || _fs
108
109 var spaces = typeof options === 'object' && options !== null
110 ? 'spaces' in options
111 ? options.spaces : this.spaces
112 : this.spaces
113
114 var str = JSON.stringify(obj, options.replacer, spaces) + '\n'
115 // not sure if fs.writeFileSync returns anything, but just in case
116 return fs.writeFileSync(file, str, options)
117}
118
119function stripBom (content) {
120 // we do this because JSON.parse would convert it to a utf8 string if encoding wasn't specified
121 if (Buffer.isBuffer(content)) content = content.toString('utf8')
122 content = content.replace(/^\uFEFF/, '')
123 return content
124}
125
126var jsonfile = {
127 spaces: null,
128 readFile: readFile,
129 readFileSync: readFileSync,
130 writeFile: writeFile,
131 writeFileSync: writeFileSync
132}
133
134module.exports = jsonfile