From d68d780951738683072bdb2c5a759a93d63698c1 Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Sat, 17 Aug 2024 12:56:35 -0400 Subject: [PATCH] Make persistent storage location a constant --- src/main.ts | 7 ++++--- src/utils/logger.ts | 12 +++++++++--- src/utils/utils.ts | 3 +-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main.ts b/src/main.ts index 16452b2..d88d940 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,13 +4,11 @@ import http from "http"; import dotenv from "dotenv"; import { Review, reviewSchema } from "./types"; import { checkFile } from "./utils"; -import { dir } from "./utils"; dotenv.config(); const origin = chalk.bold(chalk.yellow("main.js: ")); const port = +process.env.PORT ?? 8080; const contentType = { "Content-Type": "application/json" }; -const fileName = dir + "data.json"; http .createServer((req, res) => { @@ -37,7 +35,10 @@ http ); data.push(valid); - fs.writeFileSync(fileName, JSON.stringify(data, null, 2)); + fs.writeFileSync( + "./persist/data.json", + JSON.stringify(data, null, 2), + ); res.writeHead(200, contentType); res.write("OK"); diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 3189494..97a40d7 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -13,15 +13,21 @@ export class Logger { } log(text: any, args?: any): void { - args ? console.log(this._main + text, args) : console.log(this._main + text); + args + ? console.log(this._main + text, args) + : console.log(this._main + text); } warn(text: any, args?: any): void { - args ? console.warn(this._wrn + this._main + text, args) : console.warn(this._wrn + this._main + text); + args + ? console.warn(this._wrn + this._main + text, args) + : console.warn(this._wrn + this._main + text); } error(text: any, args?: any): void { - args ? console.error(this._err + this._main + text, args) : console.error(this._err + this._main + text); + args + ? console.error(this._err + this._main + text, args) + : console.error(this._err + this._main + text); } } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 6ccacc7..07ca094 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,9 +1,8 @@ import fs from "fs"; import { Review } from "../types"; -export const dir = "./persist/"; - export function checkFile(file: string, encoding: fs.EncodingOption): Review[] { + const dir = "./persist/"; const fullPath = dir + file; let final: Review[];