index.js 3.1 KB

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