Skip to content

Commit

Permalink
JSON updates (#768)
Browse files Browse the repository at this point in the history
* Upgrade jsonfile

Fixes #745

* Test from main entry point

* Make outputJson() mutation-proof

Fixes #702

* Update outputJsonSync() to match async implementation
  • Loading branch information
RyanZim committed Mar 11, 2020
1 parent 92388f2 commit a149f82
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
5 changes: 2 additions & 3 deletions lib/json/__tests__/output-json-sync.test.js
Expand Up @@ -5,7 +5,6 @@ const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')
const outputJsonSync = require('../output-json-sync')

/* global beforeEach, describe, it */

Expand All @@ -23,7 +22,7 @@ describe('json', () => {
assert(!fs.existsSync(file))

const data = { name: 'JP' }
outputJsonSync(file, data)
fse.outputJsonSync(file, data)

assert(fs.existsSync(file))
const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
Expand All @@ -39,7 +38,7 @@ describe('json', () => {
const replacer = (k, v) => v === 'JP' ? 'Jon Paul' : v
const data = { name: 'JP' }

outputJsonSync(file, data, { replacer })
fse.outputJsonSync(file, data, { replacer })
const newData = JSON.parse(fs.readFileSync(file, 'utf8'))

assert.strictEqual(newData.name, 'Jon Paul')
Expand Down
27 changes: 23 additions & 4 deletions lib/json/__tests__/output-json.test.js
Expand Up @@ -5,7 +5,6 @@ const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')
const outputJson = require('../output-json')

/* global beforeEach, describe, it */

Expand All @@ -23,7 +22,7 @@ describe('json', () => {
assert(!fs.existsSync(file))

const data = { name: 'JP' }
outputJson(file, data, err => {
fse.outputJson(file, data, err => {
if (err) return done(err)

assert(fs.existsSync(file))
Expand All @@ -34,12 +33,32 @@ describe('json', () => {
})
})

it('should be mutation-proof', async () => {
const dir = path.join(TEST_DIR, 'this-dir', 'certanly-does-not', 'exist')
const file = path.join(dir, 'file.json')
assert(!fs.existsSync(dir), 'directory cannot exist')

const name = 'JP'
const data = { name }
const promise = fse.outputJson(file, data)
// Mutate data right after call
data.name = 'Ryan'
// now await for the call to finish
await promise

assert(fs.existsSync(file))
const newData = JSON.parse(fs.readFileSync(file, 'utf8'))

// mutation did not change data
assert.strictEqual(newData.name, name)
})

it('should support Promises', () => {
const file = path.join(TEST_DIR, 'this-dir', 'prob-does-not', 'exist', 'file.json')
assert(!fs.existsSync(file))

const data = { name: 'JP' }
return outputJson(file, data)
return fse.outputJson(file, data)
})

describe('> when an option is passed, like JSON replacer', () => {
Expand All @@ -50,7 +69,7 @@ describe('json', () => {
const replacer = (k, v) => v === 'JP' ? 'Jon Paul' : v
const data = { name: 'JP' }

outputJson(file, data, { replacer }, err => {
fse.outputJson(file, data, { replacer }, err => {
assert.ifError(err)
const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
assert.strictEqual(newData.name, 'Jon Paul')
Expand Down
2 changes: 1 addition & 1 deletion lib/json/index.js
@@ -1,6 +1,6 @@
'use strict'

const u = require('universalify').fromCallback
const u = require('universalify').fromPromise
const jsonFile = require('./jsonfile')

jsonFile.outputJson = u(require('./output-json'))
Expand Down
5 changes: 2 additions & 3 deletions lib/json/jsonfile.js
@@ -1,12 +1,11 @@
'use strict'

const u = require('universalify').fromCallback
const jsonFile = require('jsonfile')

module.exports = {
// jsonfile exports
readJson: u(jsonFile.readFile),
readJson: jsonFile.readFile,
readJsonSync: jsonFile.readFileSync,
writeJson: u(jsonFile.writeFile),
writeJson: jsonFile.writeFile,
writeJsonSync: jsonFile.writeFileSync
}
14 changes: 4 additions & 10 deletions lib/json/output-json-sync.js
@@ -1,18 +1,12 @@
'use strict'

const fs = require('graceful-fs')
const path = require('path')
const mkdir = require('../mkdirs')
const jsonFile = require('./jsonfile')
const { stringify } = require('jsonfile/utils')
const { outputFileSync } = require('../output')

function outputJsonSync (file, data, options) {
const dir = path.dirname(file)
const str = stringify(data, options)

if (!fs.existsSync(dir)) {
mkdir.mkdirsSync(dir)
}

jsonFile.writeJsonSync(file, data, options)
outputFileSync(file, str, options)
}

module.exports = outputJsonSync
25 changes: 5 additions & 20 deletions lib/json/output-json.js
@@ -1,27 +1,12 @@
'use strict'

const path = require('path')
const mkdir = require('../mkdirs')
const pathExists = require('../path-exists').pathExists
const jsonFile = require('./jsonfile')
const { stringify } = require('jsonfile/utils')
const { outputFile } = require('../output')

function outputJson (file, data, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
async function outputJson (file, data, options = {}) {
const str = stringify(data, options)

const dir = path.dirname(file)

pathExists(dir, (err, itDoes) => {
if (err) return callback(err)
if (itDoes) return jsonFile.writeJson(file, data, options, callback)

mkdir.mkdirs(dir, err => {
if (err) return callback(err)
jsonFile.writeJson(file, data, options, callback)
})
})
await outputFile(file, str, options)
}

module.exports = outputJson
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"dependencies": {
"at-least-node": "^1.0.0",
"graceful-fs": "^4.2.0",
"jsonfile": "^4.0.0",
"jsonfile": "^6.0.1",
"universalify": "^1.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit a149f82

Please sign in to comment.