@visulima/fs
Version:
Human friendly file system utilities for Node.js
31 lines (30 loc) • 1.36 kB
TypeScript
import type { WriteFileOptions } from "../types.d.ts";
/**
* Synchronously writes data to a file, replacing the file if it already exists.
* This function includes safeguards like writing to a temporary file first and then renaming, and handling permissions.
* @param path The path to the file to write. Can be a file URL or a string path.
* @param content The data to write. Can be a string, Buffer, ArrayBuffer, or ArrayBufferView.
* @param options Optional configuration for writing the file. See {@link WriteFileOptions}.
* @returns void
* @example
* ```javascript
* import { writeFileSync } from "@visulima/fs";
* import { join } from "node:path";
*
* const writeMyFileSync = () => {
* try {
* writeFileSync(join("/tmp", "my-new-file-sync.txt"), "Hello World Synchronously!");
* console.log("File written successfully (sync).");
*
* writeFileSync(join("/tmp", "another-file-sync.txt"), "Some other sync content", { encoding: 'utf16le', mode: 0o600 });
* console.log("Another file written with specific options (sync).");
* } catch (error) {
* console.error("Failed to write file (sync):", error);
* }
* };
*
* writeMyFileSync();
* ```
*/
declare const writeFileSync: (path: URL | string, content: ArrayBuffer | ArrayBufferView | string, options?: WriteFileOptions) => void;
export default writeFileSync;