Skip to content

Commit

Permalink
feat: replace with promise fileread, add trycatch, fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
olzhik11 committed Jan 31, 2025
1 parent ae7e78b commit 7ebd19e
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions frontend/lib/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import path from "path";
const allowedEmailsFileName = "allowed-emails.json";

export const getEmailsConfig = async (): Promise<string[] | false> => {
const filePath = path.join(process.cwd(), allowedEmailsFileName);
try {
const filePath = path.join(process.cwd(), allowedEmailsFileName);

if (!fs.existsSync(filePath)) {
return false;
}

const fileContent = fs.readFileSync(filePath, "utf-8");
if (!fs.existsSync(filePath)) {
return false;
}

const jsonData = JSON.parse(fileContent) as { emails?: string[] };
const fileContent = await fs.promises.readFile(filePath, "utf-8");
const jsonData = JSON.parse(fileContent) as { emails?: string[] };

if (!jsonData?.emails) {
return jsonData?.emails ?? [];
} catch (e) {
throw new Error(`Invalid file format for ${allowedEmailsFileName}`);
}

return jsonData.emails;
};

0 comments on commit 7ebd19e

Please sign in to comment.