// File System APIs. Some custom APIs, but mostly a re-export a mix of built-in // Node.js sync+promise fs methods. For convenince. export { existsSync, readdir, readdirSync, readFile, readFileSync, rm, rmSync, stat, statSync, writeFile, writeFileSync, }; export function mkdir(dir: string) { return nodeMkdir(dir, { recursive: true }); } export function mkdirSync(dir: string) { return nodeMkdirSync(dir, { recursive: true }); } export type WriteFileAsyncOptions = Parameters[2]; export async function writeMkdir( file: string, contents: Buffer | string, options?: WriteFileAsyncOptions, ) { await mkdir(path.dirname(file)); return writeFile(file, contents, options); } export function writeMkdirSync(file: string, contents: Buffer | string) { mkdirSync(path.dirname(file)); return writeFileSync(file, contents); } export function readDirRecOptionalSync(dir: string) { try { return readdirSync(dir, { recursive: true, encoding: "utf8" }); } catch (err: any) { if (err.code === "ENOENT") return []; throw err; } } export async function readJson(file: string) { return JSON.parse(await readFile(file, "utf-8")) as T; } export function readJsonSync(file: string) { return JSON.parse(readFileSync(file, "utf-8")) as T; } import * as path from "node:path"; import { existsSync, mkdirSync as nodeMkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs"; import { mkdir as nodeMkdir, readdir, readFile, rm, stat, writeFile, } from "node:fs/promises";