An open API service indexing awesome lists of open source software.

https://github.com/bconnorwhite/write-json-safe

Write formatted JSON to a file, and create parent directories if necessary.
https://github.com/bconnorwhite/write-json-safe

json node nodejs typescript

Last synced: 2 months ago
JSON representation

Write formatted JSON to a file, and create parent directories if necessary.

Awesome Lists containing this project

README

          


write-json-safe



NPM


TypeScript


Coverage Status


Write formatted JSON to a file.


_If I should maintain this repo, please ⭐️_

GitHub stars

_DM me on [Twitter](https://twitter.com/bconnorwhite) if you have questions or suggestions._

Twitter

---

## Installation

```sh
yarn add write-json-safe
```

```sh
npm install write-json-safe
```

```sh
pnpm add write-json-safe
```

## Usage

```ts
import { writeJSON, writeJSONSync, Options, OptionalJSONValue } from "write-json-safe";

function writeJSON(path: string, content?: OptionalJSONValue, options?: Options): Promise;

function writeJSONSync(path: string, content?: OptionalJSONValue, options?: Options): boolean;

type Options = {
/**
* Output formatted JSON. Default: `true`
*/
pretty?: boolean;
/**
* Recursively create parent directories if needed. Default: `true`
*/
recursive?: boolean;
/**
* Ensure file ends with a newline. Default: `true`
*/
appendNewline?: boolean;
/**
* Write even if file already exists. Default: `true`
*/
overwrite?: boolean;
};
```


Dependenciesdependencies

- [stringify-json-object](https://www.npmjs.com/package/stringify-json-object): Stringify and format a JSON object
- [write-file-safe](https://www.npmjs.com/package/write-file-safe): Write files atomically, and create parent directories if necessary


Dev Dependencies

- [@types/mock-fs](https://www.npmjs.com/package/@types/mock-fs): TypeScript definitions for mock-fs
- [autorepo](https://www.npmjs.com/package/autorepo): Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.
- [mock-fs](https://www.npmjs.com/package/mock-fs): A configurable mock file system. You know, for testing.
- [read-file-safe](https://www.npmjs.com/package/read-file-safe): Read files without try catch


License license

[MIT](https://opensource.org/licenses/MIT)


## Related Packages

- [types-json](https://www.npmjs.com/package/types-json): Type checking for JSON values
- [fs-safe](https://www.npmjs.com/package/fs-safe): A simple fs wrapper that doesn't throw
- [read-json-safe](https://www.npmjs.com/package/read-json-safe): Read JSON files without try catch
- [write-file-safe](https://www.npmjs.com/package/write-file-safe): Write files, and create parent directories if necessary
- [write-md-safe](https://www.npmjs.com/package/write-md-safe): Write markdown files from a [Marked](https://www.npmjs.com/package/marked) token list or string