UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

31 lines (30 loc) 1.4 kB
import type { WriteJsonOptions } from "../types.d.ts"; /** * Asynchronously writes an object to a JSON file. * Handles indentation detection, custom stringifiers, and gracefully manages existing files. * @param path The path to the JSON file to write. Can be a file URL or a string path. * @param data The data to serialize and write. Can be any JavaScript value that can be stringified by `JSON.stringify` or a custom stringifier. * @param options Optional configuration for writing the JSON file. See {@link WriteJsonOptions}. * @returns A promise that resolves when the JSON file has been written. * @example * ```javascript * import { writeJson } from "@visulima/fs"; * import { join } from "node:path"; * * const writeMyJson = async () => { * try { * await writeJson(join("/tmp", "my-config.json"), { setting: "enabled", value: 123 }); * console.log("JSON file written successfully."); * * await writeJson(join("/tmp", "another-config.json"), { user: "test", id: "abc" }, { indent: 2, replacer: ["user"] }); * console.log("Another JSON file written with specific options (indent 2, only 'user' key)."); * } catch (error) { * console.error("Failed to write JSON file:", error); * } * }; * * writeMyJson(); * ``` */ declare const writeJson: (path: URL | string, data: unknown, options?: WriteJsonOptions) => Promise<void>; export default writeJson;