Skip to content

Commit a149f82

Browse files
authored
JSON updates (#768)
* Upgrade jsonfile Fixes #745 * Test from main entry point * Make outputJson() mutation-proof Fixes #702 * Update outputJsonSync() to match async implementation
1 parent 92388f2 commit a149f82

File tree

7 files changed

+38
-42
lines changed

7 files changed

+38
-42
lines changed

lib/json/__tests__/output-json-sync.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const os = require('os')
55
const fse = require(process.cwd())
66
const path = require('path')
77
const assert = require('assert')
8-
const outputJsonSync = require('../output-json-sync')
98

109
/* global beforeEach, describe, it */
1110

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

2524
const data = { name: 'JP' }
26-
outputJsonSync(file, data)
25+
fse.outputJsonSync(file, data)
2726

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

42-
outputJsonSync(file, data, { replacer })
41+
fse.outputJsonSync(file, data, { replacer })
4342
const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
4443

4544
assert.strictEqual(newData.name, 'Jon Paul')

lib/json/__tests__/output-json.test.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const os = require('os')
55
const fse = require(process.cwd())
66
const path = require('path')
77
const assert = require('assert')
8-
const outputJson = require('../output-json')
98

109
/* global beforeEach, describe, it */
1110

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

2524
const data = { name: 'JP' }
26-
outputJson(file, data, err => {
25+
fse.outputJson(file, data, err => {
2726
if (err) return done(err)
2827

2928
assert(fs.existsSync(file))
@@ -34,12 +33,32 @@ describe('json', () => {
3433
})
3534
})
3635

36+
it('should be mutation-proof', async () => {
37+
const dir = path.join(TEST_DIR, 'this-dir', 'certanly-does-not', 'exist')
38+
const file = path.join(dir, 'file.json')
39+
assert(!fs.existsSync(dir), 'directory cannot exist')
40+
41+
const name = 'JP'
42+
const data = { name }
43+
const promise = fse.outputJson(file, data)
44+
// Mutate data right after call
45+
data.name = 'Ryan'
46+
// now await for the call to finish
47+
await promise
48+
49+
assert(fs.existsSync(file))
50+
const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
51+
52+
// mutation did not change data
53+
assert.strictEqual(newData.name, name)
54+
})
55+
3756
it('should support Promises', () => {
3857
const file = path.join(TEST_DIR, 'this-dir', 'prob-does-not', 'exist', 'file.json')
3958
assert(!fs.existsSync(file))
4059

4160
const data = { name: 'JP' }
42-
return outputJson(file, data)
61+
return fse.outputJson(file, data)
4362
})
4463

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

53-
outputJson(file, data, { replacer }, err => {
72+
fse.outputJson(file, data, { replacer }, err => {
5473
assert.ifError(err)
5574
const newData = JSON.parse(fs.readFileSync(file, 'utf8'))
5675
assert.strictEqual(newData.name, 'Jon Paul')

lib/json/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const u = require('universalify').fromCallback
3+
const u = require('universalify').fromPromise
44
const jsonFile = require('./jsonfile')
55

66
jsonFile.outputJson = u(require('./output-json'))

lib/json/jsonfile.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict'
22

3-
const u = require('universalify').fromCallback
43
const jsonFile = require('jsonfile')
54

65
module.exports = {
76
// jsonfile exports
8-
readJson: u(jsonFile.readFile),
7+
readJson: jsonFile.readFile,
98
readJsonSync: jsonFile.readFileSync,
10-
writeJson: u(jsonFile.writeFile),
9+
writeJson: jsonFile.writeFile,
1110
writeJsonSync: jsonFile.writeFileSync
1211
}

lib/json/output-json-sync.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
'use strict'
22

3-
const fs = require('graceful-fs')
4-
const path = require('path')
5-
const mkdir = require('../mkdirs')
6-
const jsonFile = require('./jsonfile')
3+
const { stringify } = require('jsonfile/utils')
4+
const { outputFileSync } = require('../output')
75

86
function outputJsonSync (file, data, options) {
9-
const dir = path.dirname(file)
7+
const str = stringify(data, options)
108

11-
if (!fs.existsSync(dir)) {
12-
mkdir.mkdirsSync(dir)
13-
}
14-
15-
jsonFile.writeJsonSync(file, data, options)
9+
outputFileSync(file, str, options)
1610
}
1711

1812
module.exports = outputJsonSync

lib/json/output-json.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
'use strict'
22

3-
const path = require('path')
4-
const mkdir = require('../mkdirs')
5-
const pathExists = require('../path-exists').pathExists
6-
const jsonFile = require('./jsonfile')
3+
const { stringify } = require('jsonfile/utils')
4+
const { outputFile } = require('../output')
75

8-
function outputJson (file, data, options, callback) {
9-
if (typeof options === 'function') {
10-
callback = options
11-
options = {}
12-
}
6+
async function outputJson (file, data, options = {}) {
7+
const str = stringify(data, options)
138

14-
const dir = path.dirname(file)
15-
16-
pathExists(dir, (err, itDoes) => {
17-
if (err) return callback(err)
18-
if (itDoes) return jsonFile.writeJson(file, data, options, callback)
19-
20-
mkdir.mkdirs(dir, err => {
21-
if (err) return callback(err)
22-
jsonFile.writeJson(file, data, options, callback)
23-
})
24-
})
9+
await outputFile(file, str, options)
2510
}
2611

2712
module.exports = outputJson

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"dependencies": {
4040
"at-least-node": "^1.0.0",
4141
"graceful-fs": "^4.2.0",
42-
"jsonfile": "^4.0.0",
42+
"jsonfile": "^6.0.1",
4343
"universalify": "^1.0.0"
4444
},
4545
"devDependencies": {

0 commit comments

Comments
 (0)