write-json-file
Advanced tools
Comparing version 2.3.0 to 3.0.0
61
index.js
@@ -10,4 +10,4 @@ 'use strict'; | ||
const init = (fn, fp, data, opts) => { | ||
if (!fp) { | ||
const init = (fn, filePath, data, options) => { | ||
if (!filePath) { | ||
throw new TypeError('Expected a filepath'); | ||
@@ -20,39 +20,40 @@ } | ||
opts = Object.assign({ | ||
options = Object.assign({ | ||
indent: '\t', | ||
sortKeys: false | ||
}, opts); | ||
}, options); | ||
if (opts.sortKeys) { | ||
if (options.sortKeys) { | ||
data = sortKeys(data, { | ||
deep: true, | ||
compare: typeof opts.sortKeys === 'function' && opts.sortKeys | ||
compare: typeof options.sortKeys === 'function' && options.sortKeys | ||
}); | ||
} | ||
return fn(fp, data, opts); | ||
return fn(filePath, data, options); | ||
}; | ||
const readFile = fp => pify(fs.readFile)(fp, 'utf8').catch(() => {}); | ||
const readFile = filePath => pify(fs.readFile)(filePath, 'utf8').catch(() => {}); | ||
const main = (fp, data, opts) => { | ||
return (opts.detectIndent ? readFile(fp) : Promise.resolve()) | ||
.then(str => { | ||
const indent = str ? detectIndent(str).indent : opts.indent; | ||
const json = JSON.stringify(data, opts.replacer, indent); | ||
const main = (filePath, data, options) => { | ||
return (options.detectIndent ? readFile(filePath) : Promise.resolve()) | ||
.then(string => { | ||
const indent = string ? detectIndent(string).indent : options.indent; | ||
const json = JSON.stringify(data, options.replacer, indent); | ||
return pify(writeFileAtomic)(fp, `${json}\n`, {mode: opts.mode}); | ||
return pify(writeFileAtomic)(filePath, `${json}\n`, {mode: options.mode}); | ||
}); | ||
}; | ||
const mainSync = (fp, data, opts) => { | ||
let indent = opts.indent; | ||
const mainSync = (filePath, data, options) => { | ||
let {indent} = options; | ||
if (opts.detectIndent) { | ||
if (options.detectIndent) { | ||
try { | ||
const file = fs.readFileSync(fp, 'utf8'); | ||
const file = fs.readFileSync(filePath, 'utf8'); | ||
// eslint-disable-next-line prefer-destructuring | ||
indent = detectIndent(file).indent; | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') { | ||
throw err; | ||
} catch (error) { | ||
if (error.code !== 'ENOENT') { | ||
throw error; | ||
} | ||
@@ -62,15 +63,17 @@ } | ||
const json = JSON.stringify(data, opts.replacer, indent); | ||
const json = JSON.stringify(data, options.replacer, indent); | ||
return writeFileAtomic.sync(fp, `${json}\n`, {mode: opts.mode}); | ||
return writeFileAtomic.sync(filePath, `${json}\n`, {mode: options.mode}); | ||
}; | ||
module.exports = (fp, data, opts) => { | ||
return makeDir(path.dirname(fp), {fs}) | ||
.then(() => init(main, fp, data, opts)); | ||
const writeJsonFile = (filePath, data, options) => { | ||
return makeDir(path.dirname(filePath), {fs}) | ||
.then(() => init(main, filePath, data, options)); | ||
}; | ||
module.exports.sync = (fp, data, opts) => { | ||
makeDir.sync(path.dirname(fp), {fs}); | ||
init(mainSync, fp, data, opts); | ||
module.exports = writeJsonFile; | ||
module.exports.default = writeJsonFile; | ||
module.exports.sync = (filePath, data, options) => { | ||
makeDir.sync(path.dirname(filePath), {fs}); | ||
init(mainSync, filePath, data, options); | ||
}; |
{ | ||
"name": "write-json-file", | ||
"version": "2.3.0", | ||
"description": "Stringify and write JSON to a file atomically", | ||
"license": "MIT", | ||
"repository": "sindresorhus/write-json-file", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"write", | ||
"json", | ||
"stringify", | ||
"file", | ||
"fs", | ||
"graceful", | ||
"stable", | ||
"sort", | ||
"newline", | ||
"indent", | ||
"atomic", | ||
"atomically" | ||
], | ||
"dependencies": { | ||
"detect-indent": "^5.0.0", | ||
"graceful-fs": "^4.1.2", | ||
"make-dir": "^1.0.0", | ||
"pify": "^3.0.0", | ||
"sort-keys": "^2.0.0", | ||
"write-file-atomic": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"tempfile": "^2.0.0", | ||
"xo": "*" | ||
} | ||
"name": "write-json-file", | ||
"version": "3.0.0", | ||
"description": "Stringify and write JSON to a file atomically", | ||
"license": "MIT", | ||
"repository": "sindresorhus/write-json-file", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"write", | ||
"json", | ||
"stringify", | ||
"file", | ||
"fs", | ||
"graceful", | ||
"stable", | ||
"sort", | ||
"newline", | ||
"indent", | ||
"atomic", | ||
"atomically" | ||
], | ||
"dependencies": { | ||
"detect-indent": "^5.0.0", | ||
"graceful-fs": "^4.1.2", | ||
"make-dir": "^1.0.0", | ||
"pify": "^4.0.0", | ||
"sort-keys": "^2.0.0", | ||
"write-file-atomic": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"tempfile": "^2.0.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"ignores": [ | ||
"index.d.ts" | ||
] | ||
} | ||
} |
@@ -20,5 +20,5 @@ # write-json-file [![Build Status](https://travis-ci.org/sindresorhus/write-json-file.svg?branch=master)](https://travis-ci.org/sindresorhus/write-json-file) | ||
writeJsonFile('foo.json', {foo: true}).then(() => { | ||
console.log('done'); | ||
}); | ||
(async () => { | ||
await writeJsonFile('foo.json', {foo: true}); | ||
})(); | ||
``` | ||
@@ -29,7 +29,7 @@ | ||
### writeJsonFile(filepath, data, [options]) | ||
### writeJsonFile(filePath, data, [options]) | ||
Returns a `Promise`. | ||
### writeJsonFile.sync(filepath, data, [options]) | ||
### writeJsonFile.sync(filePath, data, [options]) | ||
@@ -57,3 +57,3 @@ #### options | ||
Type: `boolean` `function`<br> | ||
Type: `boolean` `Function`<br> | ||
Default: `false` | ||
@@ -66,3 +66,3 @@ | ||
Type: `function` | ||
Type: `Function` | ||
@@ -69,0 +69,0 @@ Passed into [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7279
5
121
+ Addedpify@4.0.1(transitive)
Updatedpify@^4.0.0