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

View File

@@ -28,40 +28,30 @@ export default (
const flagConfig: Record<string, boolean> = {}
for (const flag of supportedFlags) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const importedModule = require(flag)
if (!importedModule.default) {
const flagSettings: FlagSettings = require(flag).default
if (!flagSettings) {
continue
}
const flagSettings: FlagSettings = importedModule.default
flagConfig[flagSettings.key] = isTruthy(flagSettings.default_val)
switch (true) {
case typeof process.env[flagSettings.env_key] !== "undefined":
if (logger) {
logger.info(
`Using flag ${flagSettings.env_key} from environment with value ${
process.env[flagSettings.env_key]
}`
)
}
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(
projectConfigFlags[flagSettings.key]
)
break
default:
flagConfig[flagSettings.key] = flagSettings.default_val
let from
if (typeof process.env[flagSettings.env_key] !== "undefined") {
from = "environment"
flagConfig[flagSettings.key] = isTruthy(process.env[flagSettings.env_key])
} else if (typeof projectConfigFlags[flagSettings.key] !== "undefined") {
from = "project config"
flagConfig[flagSettings.key] = isTruthy(
projectConfigFlags[flagSettings.key]
)
}
if (logger && from) {
logger.info(
`Using flag ${flagSettings.env_key} from ${from} with value ${
flagConfig[flagSettings.key]
}`
)
}
if (flagConfig[flagSettings.key]) {