21 lines
482 B
TypeScript
21 lines
482 B
TypeScript
import fs from "fs";
|
|
|
|
export function ensureDir(path: string): void {
|
|
if (!fs.existsSync(path)) {
|
|
fs.mkdirSync(path, { recursive: true });
|
|
}
|
|
}
|
|
|
|
export function readJsonFile<T>(path: string, fallback: T): T {
|
|
try {
|
|
const raw = fs.readFileSync(path, "utf-8");
|
|
return JSON.parse(raw) as T;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function writeJsonFile(path: string, value: unknown): void {
|
|
fs.writeFileSync(path, JSON.stringify(value, null, 2), "utf-8");
|
|
}
|