simple-review-server/src/utils.ts

22 lines
490 B
TypeScript
Raw Normal View History

2024-08-16 15:04:52 -04:00
import fs from "fs";
import { Review } from "./types";
export const dir = "./persist/";
export function checkFile(file: string, encoding: fs.EncodingOption): Review[] {
const fullPath = dir + file;
let final: Review[];
if (fs.existsSync(dir)) {
const data = fs.readFileSync(fullPath, encoding);
final = !data.toString().trim() ? [] : JSON.parse(data.toString());
} else {
fs.mkdirSync(dir);
fs.createWriteStream(fullPath);
final = [];
}
return final;
}