@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
44 lines (42 loc) • 1.4 kB
JavaScript
import { existsSync, readFileSync as readFileSync$1 } from "node:fs";
import { readFile as readFile$1 } from "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 readFileSync$1(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 readFile$1(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 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 existsSync(path) ? readFile(path) : "";
}
//#endregion
export { readFile, readFileIfExisting, readFileIfExistingSync, readFileSync };
//# sourceMappingURL=read-file.mjs.map