UNPKG

@thi.ng/file-io

Version:

Assorted file I/O utils (w/ logging support) for NodeJS/Bun

34 lines (33 loc) 920 B
import { isArray } from "@thi.ng/checks/is-array"; import { readFileSync } from "node:fs"; import { readFile } from "node:fs/promises"; import { maskedPath } from "./mask.js"; import { writeFile, writeFileAsync } from "./write.js"; const readText = (path, logger, encoding = "utf-8") => { logger?.debug("reading file:", maskedPath(path)); return readFileSync(path, encoding); }; const readTextAsync = (path, logger, encoding = "utf-8") => { logger?.debug("reading file:", maskedPath(path)); return readFile(path, encoding); }; const writeText = (path, body, logger, dryRun = false) => writeFile( path, isArray(body) ? body.join("\n") : body, "utf-8", logger, dryRun ); const writeTextAsync = (path, body, logger, dryRun = false) => writeFileAsync( path, isArray(body) ? body.join("\n") : body, "utf-8", logger, dryRun ); export { readText, readTextAsync, writeText, writeTextAsync };