22 lines
490 B
TypeScript
22 lines
490 B
TypeScript
|
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;
|
||
|
}
|