UNPKG

@stryke/fs

Version:

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

48 lines (46 loc) 1.61 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); let node_fs = require("node:fs"); let node_fs_promises = require("node:fs/promises"); //#region src/read-file.ts /** * Read the given content to the given file path * * @param filePath - The file path to write to */ const readFileSync = (filePath) => { if (!filePath) throw new Error("No file path provided to read data"); return (0, node_fs.readFileSync)(filePath, { encoding: "utf8" }); }; /** * Read the given content to the given file path * * @param filePath - The file path to read to */ const readFile = async (filePath) => { if (!filePath) throw new Error("No file path provided to read data"); return (0, node_fs_promises.readFile)(filePath, { encoding: "utf8" }); }; /** * Reads a file if it exists, otherwise returns an empty string. * * @param path - The path to the file to read. * @returns The content of the file if it exists, otherwise an empty string. */ function readFileIfExistingSync(path) { return (0, node_fs.existsSync)(path) ? readFileSync(path) : ""; } /** * Reads a file if it exists, otherwise returns an empty string. * * @param path - The path to the file to read. * @returns The content of the file if it exists, otherwise an empty string. */ async function readFileIfExisting(path) { return (0, node_fs.existsSync)(path) ? readFile(path) : ""; } //#endregion exports.readFile = readFile; exports.readFileIfExisting = readFileIfExisting; exports.readFileIfExistingSync = readFileIfExistingSync; exports.readFileSync = readFileSync;