chore(medusa): Feature flag loader simplify, deduplicate and increase readability (#2025)

This commit is contained in:
Adrien de Peretti
2022-08-10 13:02:26 +02:00
committed by GitHub
parent 900260c5b9
commit 987ce2ab6d
@@ -28,40 +28,30 @@ export default (
const flagConfig: Record<string, boolean> = {} const flagConfig: Record<string, boolean> = {}
for (const flag of supportedFlags) { for (const flag of supportedFlags) {
// eslint-disable-next-line @typescript-eslint/no-var-requires // eslint-disable-next-line @typescript-eslint/no-var-requires
const importedModule = require(flag) const flagSettings: FlagSettings = require(flag).default
if (!importedModule.default) { if (!flagSettings) {
continue continue
} }
const flagSettings: FlagSettings = importedModule.default flagConfig[flagSettings.key] = isTruthy(flagSettings.default_val)
switch (true) { let from
case typeof process.env[flagSettings.env_key] !== "undefined": if (typeof process.env[flagSettings.env_key] !== "undefined") {
if (logger) { from = "environment"
logger.info( flagConfig[flagSettings.key] = isTruthy(process.env[flagSettings.env_key])
`Using flag ${flagSettings.env_key} from environment with value ${ } else if (typeof projectConfigFlags[flagSettings.key] !== "undefined") {
process.env[flagSettings.env_key] from = "project config"
}`
)
}
flagConfig[flagSettings.key] = isTruthy(
process.env[flagSettings.env_key]
)
break
case typeof projectConfigFlags[flagSettings.key] !== "undefined":
if (logger) {
logger.info(
`Using flag ${flagSettings.key} from project config with value ${
projectConfigFlags[flagSettings.key]
}`
)
}
flagConfig[flagSettings.key] = isTruthy( flagConfig[flagSettings.key] = isTruthy(
projectConfigFlags[flagSettings.key] projectConfigFlags[flagSettings.key]
) )
break }
default:
flagConfig[flagSettings.key] = flagSettings.default_val if (logger && from) {
logger.info(
`Using flag ${flagSettings.env_key} from ${from} with value ${
flagConfig[flagSettings.key]
}`
)
} }
if (flagConfig[flagSettings.key]) { if (flagConfig[flagSettings.key]) {