From 7bcf6923fe6da3817ad2c0c2db41d79c5356b20f Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Wed, 24 Aug 2022 18:42:10 +0200 Subject: [PATCH] Replace dynamic-require with fs readFileSync --- src/config/configuration.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/config/configuration.js b/src/config/configuration.js index cafaa07..ba0d0a8 100644 --- a/src/config/configuration.js +++ b/src/config/configuration.js @@ -1,3 +1,4 @@ +import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; @@ -10,8 +11,7 @@ const __dirname = path.dirname(__filename); class Config { constructor() { this.location = Config.determineLocation(); - // eslint-disable-next-line import/no-dynamic-require, global-require - this.fields = require(`${this.location}`); + this.fields = Config.readFileContents(this.location); } static getInstance() { @@ -25,6 +25,17 @@ class Config { return path.join(__dirname, "..", "..", process.env.SEASONED_CONFIG); } + static readFileContents(location) { + let content = {}; + try { + content = JSON.parse(fs.readFileSync(location, { encoding: "utf-8" })); + } catch (err) { + console.error(`Error loading configuration file at path: ${location}`); + } + + return content; + } + get(section, option) { if ( this.fields[section] === undefined ||