UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

30 lines (29 loc) 1.36 kB
import type { WriteJsonOptions } from "../types.d.ts"; /** * Synchronously 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}. * @example * ```javascript * import { writeJsonSync } from "@visulima/fs"; * import { join } from "node:path"; * * const writeMyJsonSync = () => { * try { * writeJsonSync(join("/tmp", "my-config-sync.json"), { setting: "enabled", value: 456 }); * console.log("JSON file written successfully (sync)."); * * writeJsonSync(join("/tmp", "another-config-sync.json"), { user: "testSync", id: "def" }, { indent: 4, replacer: ["id"] }); * console.log("Another JSON file written with specific options (sync, indent 4, only 'id' key)."); * } catch (error) { * console.error("Failed to write JSON file (sync):", error); * } * }; * * writeMyJsonSync(); * ``` */ declare const writeJsonSync: (path: URL | string, data: unknown, options?: WriteJsonOptions) => void; export default writeJsonSync;