write-json-file
Advanced tools
Comparing version 4.3.0 to 5.0.0
120
index.d.ts
@@ -1,81 +0,77 @@ | ||
declare namespace writeJsonFile { | ||
type Replacer = (this: unknown, key: string, value: any) => unknown; | ||
type SortKeys = (a: string, b: string) => number; | ||
export type Replacer = (this: unknown, key: string, value: unknown) => unknown; | ||
export type SortKeys = (a: string, b: string) => number; | ||
interface Options { | ||
/** | ||
Indentation as a string or number of spaces. Pass in `undefined` for no formatting. | ||
export interface Options { | ||
/** | ||
Indentation as a string or number of spaces. | ||
@default '\t' | ||
*/ | ||
readonly indent?: string | number | undefined; | ||
Pass in `undefined` for no formatting. | ||
/** | ||
Detect indentation automatically if the file exists. | ||
@default '\t' | ||
*/ | ||
readonly indent?: string | number | undefined; | ||
@default false | ||
*/ | ||
readonly detectIndent?: boolean; | ||
/** | ||
Detect indentation automatically if the file exists. | ||
/** | ||
Sort the keys recursively. Optionally pass in a compare function. | ||
@default false | ||
*/ | ||
readonly detectIndent?: boolean; | ||
@default false | ||
*/ | ||
readonly sortKeys?: boolean | SortKeys; | ||
/** | ||
Sort the keys recursively. | ||
/** | ||
Passed into `JSON.stringify`. | ||
*/ | ||
readonly replacer?: Replacer | ReadonlyArray<number | string>; | ||
Optionally pass in a compare function. | ||
/** | ||
Mode used when writing the file. | ||
@default false | ||
*/ | ||
readonly sortKeys?: boolean | SortKeys; | ||
@default 0o666 | ||
*/ | ||
readonly mode?: number; | ||
} | ||
} | ||
/** | ||
Passed into `JSON.stringify`. | ||
*/ | ||
readonly replacer?: Replacer | ReadonlyArray<number | string>; | ||
declare const writeJsonFile: { | ||
/** | ||
Stringify and write JSON to a file atomically. | ||
The mode used when writing the file. | ||
Creates directories for you as needed. | ||
@default 0o666 | ||
*/ | ||
readonly mode?: number; | ||
} | ||
@example | ||
``` | ||
import writeJsonFile = require('write-json-file'); | ||
/** | ||
Stringify and write JSON to a file atomically. | ||
(async () => { | ||
await writeJsonFile('foo.json', {foo: true}); | ||
})(); | ||
``` | ||
*/ | ||
( | ||
filePath: string, | ||
data: unknown, | ||
options?: writeJsonFile.Options | ||
): Promise<void>; | ||
Creates directories for you as needed. | ||
/** | ||
Stringify and write JSON to a file atomically. | ||
@example | ||
``` | ||
import {writeJsonFile} from 'write-json-file'; | ||
Creates directories for you as needed. | ||
await writeJsonFile('foo.json', {foo: true}); | ||
``` | ||
*/ | ||
export function writeJsonFile( | ||
filePath: string, | ||
data: unknown, | ||
options?: Options | ||
): Promise<void>; | ||
@example | ||
``` | ||
import writeJsonFile = require('write-json-file'); | ||
/** | ||
Stringify and write JSON to a file atomically. | ||
writeJsonFile.sync('foo.json', {foo: true}); | ||
``` | ||
*/ | ||
sync( | ||
filePath: string, | ||
data: unknown, | ||
options?: writeJsonFile.Options | ||
): void; | ||
}; | ||
Creates directories for you as needed. | ||
export = writeJsonFile; | ||
@example | ||
``` | ||
import {writeJsonFileSync} from 'write-json-file'; | ||
writeJsonFileSync('foo.json', {foo: true}); | ||
``` | ||
*/ | ||
export function writeJsonFileSync( | ||
filePath: string, | ||
data: unknown, | ||
options?: Options | ||
): void; |
41
index.js
@@ -1,14 +0,9 @@ | ||
'use strict'; | ||
const {promisify} = require('util'); | ||
const path = require('path'); | ||
const fs = require('graceful-fs'); | ||
const writeFileAtomic = require('write-file-atomic'); | ||
const sortKeys = require('sort-keys'); | ||
const makeDir = require('make-dir'); | ||
const detectIndent = require('detect-indent'); | ||
const isPlainObj = require('is-plain-obj'); | ||
import path from 'node:path'; | ||
import fs, {promises as fsPromises} from 'node:fs'; | ||
import writeFileAtomic from 'write-file-atomic'; | ||
import sortKeys from 'sort-keys'; | ||
import detectIndent from 'detect-indent'; | ||
import isPlainObj from 'is-plain-obj'; | ||
const readFile = promisify(fs.readFile); | ||
const init = (fn, filePath, data, options) => { | ||
const init = (function_, filePath, data, options) => { | ||
if (!filePath) { | ||
@@ -25,3 +20,3 @@ throw new TypeError('Expected a filepath'); | ||
sortKeys: false, | ||
...options | ||
...options, | ||
}; | ||
@@ -32,7 +27,7 @@ | ||
deep: true, | ||
compare: typeof options.sortKeys === 'function' ? options.sortKeys : undefined | ||
compare: typeof options.sortKeys === 'function' ? options.sortKeys : undefined, | ||
}); | ||
} | ||
return fn(filePath, data, options); | ||
return function_(filePath, data, options); | ||
}; | ||
@@ -44,3 +39,3 @@ | ||
try { | ||
const file = await readFile(filePath, 'utf8'); | ||
const file = await fsPromises.readFile(filePath, 'utf8'); | ||
if (!file.endsWith('\n')) { | ||
@@ -87,10 +82,10 @@ trailingNewline = ''; | ||
module.exports = async (filePath, data, options) => { | ||
await makeDir(path.dirname(filePath), {fs}); | ||
return init(main, filePath, data, options); | ||
}; | ||
export async function writeJsonFile(filePath, data, options) { | ||
await fsPromises.mkdir(path.dirname(filePath), {recursive: true}); | ||
await init(main, filePath, data, options); | ||
} | ||
module.exports.sync = (filePath, data, options) => { | ||
makeDir.sync(path.dirname(filePath), {fs}); | ||
export function writeJsonFileSync(filePath, data, options) { | ||
fs.mkdirSync(path.dirname(filePath), {recursive: true}); | ||
init(mainSync, filePath, data, options); | ||
}; | ||
} |
{ | ||
"name": "write-json-file", | ||
"version": "4.3.0", | ||
"version": "5.0.0", | ||
"description": "Stringify and write JSON to a file atomically", | ||
@@ -11,6 +11,8 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8.3" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -39,15 +41,13 @@ "scripts": { | ||
"dependencies": { | ||
"detect-indent": "^6.0.0", | ||
"graceful-fs": "^4.1.15", | ||
"is-plain-obj": "^2.0.0", | ||
"make-dir": "^3.0.0", | ||
"sort-keys": "^4.0.0", | ||
"write-file-atomic": "^3.0.0" | ||
"detect-indent": "^7.0.0", | ||
"is-plain-obj": "^4.0.0", | ||
"sort-keys": "^5.0.0", | ||
"write-file-atomic": "^3.0.3" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tempy": "^0.3.0", | ||
"tsd": "^0.8.0", | ||
"xo": "^0.25.3" | ||
"ava": "^3.15.0", | ||
"tempy": "^1.0.1", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# write-json-file [![Build Status](https://travis-ci.org/sindresorhus/write-json-file.svg?branch=master)](https://travis-ci.org/sindresorhus/write-json-file) | ||
# write-json-file | ||
@@ -16,7 +16,5 @@ > Stringify and write JSON to a file [atomically](https://github.com/npm/write-file-atomic) | ||
```js | ||
const writeJsonFile = require('write-json-file'); | ||
import {writeJsonFile} from 'write-json-file'; | ||
(async () => { | ||
await writeJsonFile('foo.json', {foo: true}); | ||
})(); | ||
await writeJsonFile('foo.json', {foo: true}); | ||
``` | ||
@@ -72,3 +70,3 @@ | ||
[Mode](https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation) used when writing the file. | ||
The [mode](https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation) used when writing the file. | ||
@@ -75,0 +73,0 @@ ## write-json-file for enterprise |
Sorry, the diff of this file is not supported yet
4
Yes
7716
130
82
+ Addeddetect-indent@7.0.1(transitive)
+ Addedis-plain-obj@4.1.0(transitive)
+ Addedsort-keys@5.0.0(transitive)
- Removedgraceful-fs@^4.1.15
- Removedmake-dir@^3.0.0
- Removeddetect-indent@6.1.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedis-plain-obj@2.1.0(transitive)
- Removedmake-dir@3.1.0(transitive)
- Removedsemver@6.3.1(transitive)
- Removedsort-keys@4.2.0(transitive)
Updateddetect-indent@^7.0.0
Updatedis-plain-obj@^4.0.0
Updatedsort-keys@^5.0.0
Updatedwrite-file-atomic@^3.0.3