@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
51 lines (49 loc) • 1.71 kB
JavaScript
import { __exportAll } from "./_virtual/_rolldown/runtime.mjs";
import { existsSync, readFileSync as readFileSync$1 } from "node:fs";
import { readFile as readFile$1 } from "node:fs/promises";
//#region src/read-file.ts
var read_file_exports = /* @__PURE__ */ __exportAll({
readFile: () => readFile,
readFileIfExisting: () => readFileIfExisting,
readFileIfExistingSync: () => readFileIfExistingSync,
readFileSync: () => readFileSync
});
/**
* 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, read_file_exports };
//# sourceMappingURL=read-file.mjs.map