Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ?? and fs.promises #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
let _fs
let fs
const universalify = require('universalify')

try {
_fs = require('graceful-fs')
fs = require('graceful-fs')
} catch (_) {
_fs = require('fs')
fs = require('fs')
}
const universalify = require('universalify')
const { stringify, stripBom } = require('./utils')

async function _readFile (file, options = {}) {
const { stringify, stripBom } = require('./utils.js')

async function readFile (file, options = {}) {
if (typeof options === 'string') {
options = { encoding: options }
}
const _fs = options.fs || fs
const shouldThrow = options.throws ?? true

const fs = options.fs || _fs

const shouldThrow = 'throws' in options ? options.throws : true

let data = await universalify.fromCallback(fs.readFile)(file, options)

data = stripBom(data)

let obj
try {
obj = JSON.parse(data, options ? options.reviver : null)
const content = await _fs.promises.readFile(file, options)
return JSON.parse(stripBom(content), options ? options.reviver : null)
} catch (err) {
if (shouldThrow) {
err.message = `${file}: ${err.message}`
Expand All @@ -31,25 +27,18 @@ async function _readFile (file, options = {}) {
return null
}
}

return obj
}

const readFile = universalify.fromPromise(_readFile)

function readFileSync (file, options = {}) {
if (typeof options === 'string') {
options = { encoding: options }
}

const fs = options.fs || _fs

const shouldThrow = 'throws' in options ? options.throws : true
const _fs = options.fs || fs
const shouldThrow = options.throws ?? true

try {
let content = fs.readFileSync(file, options)
content = stripBom(content)
return JSON.parse(content, options.reviver)
const content = _fs.readFileSync(file, options)
return JSON.parse(stripBom(content), options.reviver)
} catch (err) {
if (shouldThrow) {
err.message = `${file}: ${err.message}`
Expand All @@ -60,28 +49,22 @@ function readFileSync (file, options = {}) {
}
}

async function _writeFile (file, obj, options = {}) {
const fs = options.fs || _fs

async function writeFile (file, obj, options = {}) {
const str = stringify(obj, options)

await universalify.fromCallback(fs.writeFile)(file, str, options)
const _fs = options.fs || fs
await _fs.promises.writeFile(file, str, options)
}

const writeFile = universalify.fromPromise(_writeFile)

function writeFileSync (file, obj, options = {}) {
const fs = options.fs || _fs

const str = stringify(obj, options)
// not sure if fs.writeFileSync returns anything, but just in case
return fs.writeFileSync(file, str, options)
const _fs = options.fs || fs
_fs.writeFileSync(file, str, options)
}

const jsonfile = {
readFile,
readFile: universalify.fromPromise(readFile),
readFileSync,
writeFile,
writeFile: universalify.fromPromise(writeFile),
writeFileSync
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"utils.js"
],
"scripts": {
"lint": "standard",
"lint": "standard --fix",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure this is used for checking for the lint issues, since it's referenced from test script. As far as I understand it would break dead CI integration.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, lint shouldn't run --fix

"test": "npm run lint && npm run unit",
"unit": "mocha"
}
Expand Down
2 changes: 1 addition & 1 deletion test/read-file-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')
const jf = require('../')
const jf = require('../index.js')

/* global describe it beforeEach afterEach */

Expand Down
2 changes: 1 addition & 1 deletion test/read-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')
const jf = require('../')
const jf = require('../index.js')

/* global describe it beforeEach afterEach */

Expand Down
2 changes: 1 addition & 1 deletion test/write-file-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')
const jf = require('../')
const jf = require('../index.js')

/* global describe it beforeEach afterEach */

Expand Down
2 changes: 1 addition & 1 deletion test/write-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const os = require('os')
const path = require('path')
const rimraf = require('rimraf')
const jf = require('../')
const jf = require('../index.js')

/* global describe it beforeEach afterEach */

Expand Down
3 changes: 3 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ function stringify (obj, { EOL = '\n', finalEOL = true, replacer = null, spaces
return str.replace(/\n/g, EOL) + EOF
}

/**
* @return {string}
*/
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')
Expand Down