UNPKG

@thi.ng/file-io

Version:

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

20 lines (19 loc) 724 B
import { isString } from "@thi.ng/checks/is-string"; import { randomID } from "@thi.ng/random/random-id"; import { realpathSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { sep } from "node:path"; import { ensureDirForFile } from "./dir.js"; import { maskedPath } from "./mask.js"; const createTempFile = (body, logger, name, ext) => { const path = tempFilePath(name, ext); logger?.info("creating temp file:", maskedPath(path)); ensureDirForFile(path); writeFileSync(path, body, isString(body) ? "utf-8" : void 0); return path; }; const tempFilePath = (name = randomID(16, "tmp-"), ext = "") => realpathSync(tmpdir()) + sep + name + ext; export { createTempFile, tempFilePath };