@thi.ng/file-io
Version:
Assorted file I/O utils (with logging support) for NodeJS/Bun
19 lines (18 loc) • 676 B
JavaScript
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";
const createTempFile = (body, logger, name, ext) => {
const path = tempFilePath(name, ext);
logger?.debug("creating temp file:", path);
ensureDirForFile(path);
writeFileSync(path, body, isString(body) ? "utf-8" : void 0);
return path;
};
const tempFilePath = (name, ext = "") => realpathSync(tmpdir()) + sep + (name || randomID(16, "tmp-")) + ext;
export {
createTempFile,
tempFilePath
};