ghaction-github-pages/node_modules/jsonfile/index.js

89 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-03-23 17:00:06 +08:00
let _fs
2019-11-15 23:20:34 +08:00
try {
_fs = require('graceful-fs')
} catch (_) {
_fs = require('fs')
}
2020-03-23 17:00:06 +08:00
const universalify = require('universalify')
const { stringify, stripBom } = require('./utils')
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
async function _readFile (file, options = {}) {
2019-11-15 23:20:34 +08:00
if (typeof options === 'string') {
2020-03-23 17:00:06 +08:00
options = { encoding: options }
2019-11-15 23:20:34 +08:00
}
2020-03-23 17:00:06 +08:00
const fs = options.fs || _fs
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
const shouldThrow = 'throws' in options ? options.throws : true
let data = await universalify.fromCallback(fs.readFile)(file, options)
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
data = stripBom(data)
let obj
try {
obj = JSON.parse(data, options ? options.reviver : null)
} catch (err) {
if (shouldThrow) {
err.message = `${file}: ${err.message}`
throw err
} else {
return null
2019-11-15 23:20:34 +08:00
}
2020-03-23 17:00:06 +08:00
}
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
return obj
2019-11-15 23:20:34 +08:00
}
2020-03-23 17:00:06 +08:00
const readFile = universalify.fromPromise(_readFile)
function readFileSync (file, options = {}) {
2019-11-15 23:20:34 +08:00
if (typeof options === 'string') {
2020-03-23 17:00:06 +08:00
options = { encoding: options }
2019-11-15 23:20:34 +08:00
}
2020-03-23 17:00:06 +08:00
const fs = options.fs || _fs
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
const shouldThrow = 'throws' in options ? options.throws : true
2019-11-15 23:20:34 +08:00
try {
2020-03-23 17:00:06 +08:00
let content = fs.readFileSync(file, options)
2019-11-15 23:20:34 +08:00
content = stripBom(content)
return JSON.parse(content, options.reviver)
} catch (err) {
if (shouldThrow) {
2020-03-23 17:00:06 +08:00
err.message = `${file}: ${err.message}`
2019-11-15 23:20:34 +08:00
throw err
} else {
return null
}
}
}
2020-03-23 17:00:06 +08:00
async function _writeFile (file, obj, options = {}) {
const fs = options.fs || _fs
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
const str = stringify(obj, options)
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
await universalify.fromCallback(fs.writeFile)(file, str, options)
2019-11-15 23:20:34 +08:00
}
2020-03-23 17:00:06 +08:00
const writeFile = universalify.fromPromise(_writeFile)
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
function writeFileSync (file, obj, options = {}) {
const fs = options.fs || _fs
2019-11-15 23:20:34 +08:00
2020-03-23 17:00:06 +08:00
const str = stringify(obj, options)
2019-11-15 23:20:34 +08:00
// not sure if fs.writeFileSync returns anything, but just in case
return fs.writeFileSync(file, str, options)
}
2020-03-23 17:00:06 +08:00
const jsonfile = {
readFile,
readFileSync,
writeFile,
writeFileSync
2019-11-15 23:20:34 +08:00
}
module.exports = jsonfile