c12
c12 copied to clipboard
Allow overriding config with environment variables
We can use destr to allow overriding any configuration using process.env.{NAME}_CONFIG_{PATH}
.
Let's same NAME is NUXT
:
-
NUXT_CONFIG_SSR=false
overridesssr
tofalse
-
NUXT_CONFIG_GENERATE:CRAWLER=true
overridesgenerate.crawler
totrue
-
NUXT_CONFIG={"ssr": false, "generate": { "crawler": true }}
does the same as two above
Notes:
-
defaults
option needs to exist in order to enable an env variable to override - Same as defu, arrays can be only extended not overridden. We might find a convention for defu to allow this with specific key prefix/suffix.
@pi0 We have a need for this as well, so happy to provide a PR if desired 🙂
Before diving into anything though, do you have any more in-depth thoughts/specs on how that should work?
Directus' configuration currently does a similar level of "automagic" type casting and nesting by looking at the env var name (so for example THIS_IS__NESTED="true"
becomes { "thisIs": { "nested": true }}
, so happy to lift some of that logic into c12
and tweak it for the updated syntax. What's your plan for "multipart" names? Like should NUXT_CONFIG_CAMEL_CASE
become { "camelCase": x }
? Curious to hear your thoughts! Also happy to chat on Discord / Google Meet or whatever else is most convenient for you 🙂
@pi0 Any update on this issue, or on @rijkvanzanten's proposal ?