simple-review-server/src/utils/functions.ts

21 lines
485 B
TypeScript
Raw Normal View History

2024-08-16 15:04:52 -04:00
import fs from "fs";
2024-08-17 01:39:16 -04:00
import { Review } from "../types";
2024-08-16 15:04:52 -04:00
export function checkFile(file: string, encoding: fs.EncodingOption): Review[] {
const dir = "./persist/";
2024-08-16 15:04:52 -04:00
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;
}