aboutsummaryrefslogtreecommitdiff
path: root/node_modules/jsonfile/index.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-24 15:10:37 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-24 15:11:17 +0200
commit7a3df06eb573d36142bd1a8e03c5ce8752d300b3 (patch)
tree70bfaea8884c374876f607774850a3a51c0cb381 /node_modules/jsonfile/index.js
parentaca1143cb9eed16cf37f04e475e4257418dd18ac (diff)
downloadwallet-core-7a3df06eb573d36142bd1a8e03c5ce8752d300b3.tar.xz
fix build issues and add typedoc
Diffstat (limited to 'node_modules/jsonfile/index.js')
-rw-r--r--node_modules/jsonfile/index.js132
1 files changed, 132 insertions, 0 deletions
diff --git a/node_modules/jsonfile/index.js b/node_modules/jsonfile/index.js
new file mode 100644
index 000000000..5ac06cfff
--- /dev/null
+++ b/node_modules/jsonfile/index.js
@@ -0,0 +1,132 @@
+var _fs
+try {
+ _fs = require('graceful-fs')
+} catch (_) {
+ _fs = require('fs')
+}
+
+function readFile (file, options, callback) {
+ if (callback == null) {
+ callback = options
+ options = {}
+ }
+
+ if (typeof options === 'string') {
+ options = {encoding: options}
+ }
+
+ options = options || {}
+ var fs = options.fs || _fs
+
+ var shouldThrow = true
+ // DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead
+ if ('passParsingErrors' in options) {
+ shouldThrow = options.passParsingErrors
+ } else if ('throws' in options) {
+ shouldThrow = options.throws
+ }
+
+ fs.readFile(file, options, function (err, data) {
+ if (err) return callback(err)
+
+ data = stripBom(data)
+
+ var obj
+ try {
+ obj = JSON.parse(data, options ? options.reviver : null)
+ } catch (err2) {
+ if (shouldThrow) {
+ err2.message = file + ': ' + err2.message
+ return callback(err2)
+ } else {
+ return callback(null, null)
+ }
+ }
+
+ callback(null, obj)
+ })
+}
+
+function readFileSync (file, options) {
+ options = options || {}
+ if (typeof options === 'string') {
+ options = {encoding: options}
+ }
+
+ var fs = options.fs || _fs
+
+ var shouldThrow = true
+ // DO NOT USE 'passParsingErrors' THE NAME WILL CHANGE!!!, use 'throws' instead
+ if ('passParsingErrors' in options) {
+ shouldThrow = options.passParsingErrors
+ } else if ('throws' in options) {
+ shouldThrow = options.throws
+ }
+
+ try {
+ var content = fs.readFileSync(file, options)
+ content = stripBom(content)
+ return JSON.parse(content, options.reviver)
+ } catch (err) {
+ if (shouldThrow) {
+ err.message = file + ': ' + err.message
+ throw err
+ } else {
+ return null
+ }
+ }
+}
+
+function writeFile (file, obj, options, callback) {
+ if (callback == null) {
+ callback = options
+ options = {}
+ }
+ options = options || {}
+ var fs = options.fs || _fs
+
+ var spaces = typeof options === 'object' && options !== null
+ ? 'spaces' in options
+ ? options.spaces : this.spaces
+ : this.spaces
+
+ var str = ''
+ try {
+ str = JSON.stringify(obj, options ? options.replacer : null, spaces) + '\n'
+ } catch (err) {
+ if (callback) return callback(err, null)
+ }
+
+ fs.writeFile(file, str, options, callback)
+}
+
+function writeFileSync (file, obj, options) {
+ options = options || {}
+ var fs = options.fs || _fs
+
+ var spaces = typeof options === 'object' && options !== null
+ ? 'spaces' in options
+ ? options.spaces : this.spaces
+ : this.spaces
+
+ var str = JSON.stringify(obj, options.replacer, spaces) + '\n'
+ // not sure if fs.writeFileSync returns anything, but just in case
+ return fs.writeFileSync(file, str, options)
+}
+
+function stripBom (content) {
+ // we do this because JSON.parse would convert it to a utf8 string if encoding wasn't specified
+ if (Buffer.isBuffer(content)) content = content.toString('utf8')
+ content = content.replace(/^\uFEFF/, '')
+ return content
+}
+
+var jsonfile = {
+ spaces: null,
+ readFile: readFile,
+ readFileSync: readFileSync,
+ writeFile: writeFile,
+ writeFileSync: writeFileSync
+}
+
+module.exports = jsonfile