simple-review-server/src/utils/functions.ts
2024-08-18 17:11:15 -04:00

21 lines
485 B
TypeScript

import fs from "fs";
import { Review } from "../types";
export function checkFile(file: string, encoding: fs.EncodingOption): Review[] {
const dir = "./persist/";
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;
}