From e091cad241a497f21e145ee461ecc5eee1212fec Mon Sep 17 00:00:00 2001 From: jlpereira Date: Mon, 28 Aug 2023 15:32:30 -0300 Subject: [PATCH] Add development configuration loader --- src/utils/loadConfiguration.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/utils/loadConfiguration.js b/src/utils/loadConfiguration.js index 8fd93b6..7aa49ba 100644 --- a/src/utils/loadConfiguration.js +++ b/src/utils/loadConfiguration.js @@ -4,10 +4,31 @@ import glob from 'glob' import defaultConfig from '../constants/defaultConfig' export const loadConfiguration = (appPath) => { - const filePaths = glob.sync(appPath + '/config/*.yml', {}) - const jsonConfig = filePaths.map((filepath) => - yaml.load(fs.readFileSync(filepath, 'utf8')) - ) + const isProd = process.env.NODE_ENV === 'production' + const filePaths = glob.sync(appPath + '/config/*.yml') + const configurationPaths = splitFilePathsByEnv(filePaths) + + const jsonConfig = [ + ...configurationPaths.prod, + ...(!isProd ? configurationPaths.dev : []) + ].map((filepath) => yaml.load(fs.readFileSync(filepath, 'utf8'))) return Object.assign({}, defaultConfig, ...jsonConfig) } + +function splitFilePathsByEnv(filepaths) { + const devExtension = '.development.yml' + + return filepaths.reduce( + (acc, current) => { + if (current.includes(devExtension)) { + acc.dev.push(current) + } else { + acc.prod.push(current) + } + + return acc + }, + { dev: [], prod: [] } + ) +}