UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

39 lines (37 loc) 1.67 kB
import { existsSync as existsSync$1 } from "./exists.mjs"; import { createDirectory, createDirectorySync } from "./helpers.mjs"; import { writeFileSync as writeFileSync$1 } from "node:fs"; import { writeFile as writeFile$1 } from "node:fs/promises"; import { correctPath } from "@stryke/path/correct-path"; import { findFilePath } from "@stryke/path/file-path-fns"; //#region src/write-file.ts /** * Write the given content to the given file path * * @param filePath - The file path to write to * @param content - The content to write to the file */ const writeFileSync = (filePath, content = "", options = {}) => { if (!filePath) throw new Error("No file path provided to write data"); const directory = findFilePath(correctPath(filePath)); if (!existsSync$1(directory)) if (options.createDirectory !== false) createDirectorySync(directory); else throw new Error(`Directory ${directory} does not exist`); writeFileSync$1(filePath, content || "", options); }; /** * Read the given content to the given file path * * @param filePath - The file path to read to * @param content - The content to write to the file * @returns The content of the file */ const writeFile = async (filePath, content = "", options = {}) => { if (!filePath) throw new Error("No file path provided to read data"); const directory = findFilePath(correctPath(filePath)); if (!existsSync$1(directory)) if (options.createDirectory !== false) await createDirectory(directory); else throw new Error(`Directory ${directory} does not exist`); return writeFile$1(filePath, content || "", options); }; //#endregion export { writeFile, writeFileSync }; //# sourceMappingURL=write-file.mjs.map