write-json-file
Advanced tools
Comparing version 3.2.0 to 4.0.0
declare namespace writeJsonFile { | ||
type Replacer = (this: unknown, key: string, value: any) => unknown; | ||
type SortKeys = (a: string, b: string) => number; | ||
type JSONStringifyable = string | number | boolean | null | object; | ||
interface Options { | ||
/** | ||
Indentation as a string or number of spaces. Pass in null for no formatting. | ||
Indentation as a string or number of spaces. Pass in `undefined` for no formatting. | ||
@default '\t' | ||
*/ | ||
readonly indent?: string | number | null; | ||
readonly indent?: string | number | undefined; | ||
@@ -31,3 +30,3 @@ /** | ||
*/ | ||
readonly replacer?: Replacer | Array<number | string>; | ||
readonly replacer?: Replacer | ReadonlyArray<number | string>; | ||
@@ -59,4 +58,4 @@ /** | ||
( | ||
filepath: string, | ||
data: writeJsonFile.JSONStringifyable, | ||
filePath: string, | ||
data: unknown, | ||
options?: writeJsonFile.Options | ||
@@ -78,11 +77,8 @@ ): Promise<void>; | ||
sync( | ||
filepath: string, | ||
data: writeJsonFile.JSONStringifyable, | ||
filePath: string, | ||
data: unknown, | ||
options?: writeJsonFile.Options | ||
): void; | ||
// TODO: Remove this for the next major release | ||
default: typeof writeJsonFile; | ||
}; | ||
export = writeJsonFile; |
44
index.js
'use strict'; | ||
const {promisify} = require('util'); | ||
const path = require('path'); | ||
@@ -7,5 +8,7 @@ const fs = require('graceful-fs'); | ||
const makeDir = require('make-dir'); | ||
const pify = require('pify'); | ||
const detectIndent = require('detect-indent'); | ||
const readFile = promisify(fs.readFile); | ||
const writeFileAtomicP = promisify(writeFileAtomic); | ||
const init = (fn, filePath, data, options) => { | ||
@@ -20,6 +23,7 @@ if (!filePath) { | ||
options = Object.assign({ | ||
options = { | ||
indent: '\t', | ||
sortKeys: false | ||
}, options); | ||
sortKeys: false, | ||
...options | ||
}; | ||
@@ -36,12 +40,19 @@ if (options.sortKeys) { | ||
const readFile = filePath => pify(fs.readFile)(filePath, 'utf8').catch(() => {}); | ||
const main = async (filePath, data, options) => { | ||
let {indent} = options; | ||
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); | ||
if (options.detectIndent) { | ||
try { | ||
const file = await readFile(filePath, 'utf8'); | ||
indent = detectIndent(file).indent; | ||
} catch (error) { | ||
if (error.code !== 'ENOENT') { | ||
throw error; | ||
} | ||
} | ||
} | ||
return pify(writeFileAtomic)(filePath, `${json}\n`, {mode: options.mode}); | ||
}); | ||
const json = JSON.stringify(data, options.replacer, indent); | ||
return writeFileAtomicP(filePath, `${json}\n`, {mode: options.mode}); | ||
}; | ||
@@ -68,10 +79,7 @@ | ||
const writeJsonFile = (filePath, data, options) => { | ||
return makeDir(path.dirname(filePath), {fs}) | ||
.then(() => init(main, filePath, data, options)); | ||
module.exports = async (filePath, data, options) => { | ||
await makeDir(path.dirname(filePath), {fs}); | ||
return init(main, filePath, data, options); | ||
}; | ||
module.exports = writeJsonFile; | ||
// TODO: Remove this for the next major release | ||
module.exports.default = writeJsonFile; | ||
module.exports.sync = (filePath, data, options) => { | ||
@@ -78,0 +86,0 @@ makeDir.sync(path.dirname(filePath), {fs}); |
{ | ||
"name": "write-json-file", | ||
"version": "3.2.0", | ||
"version": "4.0.0", | ||
"description": "Stringify and write JSON to a file atomically", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -38,7 +38,6 @@ "scripts": { | ||
"dependencies": { | ||
"detect-indent": "^5.0.0", | ||
"detect-indent": "^6.0.0", | ||
"graceful-fs": "^4.1.15", | ||
"make-dir": "^2.1.0", | ||
"pify": "^4.0.1", | ||
"sort-keys": "^2.0.0", | ||
"make-dir": "^3.0.0", | ||
"sort-keys": "^3.0.0", | ||
"write-file-atomic": "^2.4.2" | ||
@@ -48,6 +47,6 @@ }, | ||
"ava": "^1.4.1", | ||
"tempfile": "^2.0.0", | ||
"tsd": "^0.7.2", | ||
"tempy": "^0.3.0", | ||
"tsd": "^0.7.3", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -36,12 +36,13 @@ # write-json-file [![Build Status](https://travis-ci.org/sindresorhus/write-json-file.svg?branch=master)](https://travis-ci.org/sindresorhus/write-json-file) | ||
Type: `Object` | ||
Type: `object` | ||
##### indent | ||
Type: `string` `number`<br> | ||
Type: `string | number`<br> | ||
Default: `'\t'` | ||
Indentation as a string or number of spaces.<br> | ||
Pass in `null` for no formatting. | ||
Indentation as a string or number of spaces. | ||
Pass in `undefined` for no formatting. | ||
##### detectIndent | ||
@@ -56,3 +57,3 @@ | ||
Type: `boolean` `Function`<br> | ||
Type: `boolean | Function`<br> | ||
Default: `false` | ||
@@ -59,0 +60,0 @@ |
5
131
85
7206
+ Addeddetect-indent@6.1.0(transitive)
+ Addedis-plain-obj@2.1.0(transitive)
+ Addedmake-dir@3.1.0(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedsort-keys@3.0.0(transitive)
- Removedpify@^4.0.1
- Removeddetect-indent@5.0.0(transitive)
- Removedis-plain-obj@1.1.0(transitive)
- Removedmake-dir@2.1.0(transitive)
- Removedpify@4.0.1(transitive)
- Removedsemver@5.7.2(transitive)
- Removedsort-keys@2.0.0(transitive)
Updateddetect-indent@^6.0.0
Updatedmake-dir@^3.0.0
Updatedsort-keys@^3.0.0