import { writeFileSync, rmSync } from "node:fs"; import { mkdtempSync } from "node:fs"; import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; export async function exists(filePath: string): Promise { try { await access(filePath); return true; } catch { return false; } } export async function readJson(filePath: string): Promise { return JSON.parse(await readFile(filePath, "utf8")) as T; } export async function writeJson(filePath: string, value: unknown): Promise { await mkdir(path.dirname(filePath), { recursive: true }); await writeFile(filePath, `${JSON.stringify(value, null, 2)}\n`, "utf8"); } export async function makeTempDir(prefix: string): Promise { return mkdtempSync(path.join(tmpdir(), prefix)); } export async function cleanupPath(filePath: string): Promise { await rm(filePath, { force: true, recursive: true }).catch(() => undefined); } export function cleanupPathSync(filePath: string): void { rmSync(filePath, { force: true, recursive: true }); } export function writeExecutable(filePath: string, content: string): void { writeFileSync(filePath, content, { encoding: "utf8", mode: 0o755 }); }