Files
dockship/src/lib/server/file.ts
T
2026-02-14 16:42:11 +01:00

34 lines
893 B
TypeScript

import { promises as fs } from "fs";
import path from "path";
export async function read<T extends object>(
template: () => T,
fp: string,
...filePaths: string[]
): Promise<T> {
const filePath = path.join(process.cwd(), fp, ...filePaths);
const dirPath = path.dirname(filePath);
try {
await fs.access(filePath);
} catch {
await fs.mkdir(dirPath, { recursive: true });
await fs.writeFile(filePath, JSON.stringify(template(), null, 2), "utf-8");
}
const content = await fs.readFile(filePath, "utf-8");
return JSON.parse(content);
}
export async function write<T extends object>(
val: T,
fp: string,
...filePaths: string[]
) {
const filePath = path.join(process.cwd(), fp, ...filePaths);
const dirPath = path.dirname(filePath);
await fs.mkdir(dirPath, { recursive: true });
await fs.writeFile(filePath, JSON.stringify(val, null, 2), "utf-8");
}