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