-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: read
.env.test
and allow overriding with nuxt.dotenv
options
- Loading branch information
Showing
7 changed files
with
95 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
NUXT_PUBLIC_TEST_VALUE=123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ export default defineNuxtConfig({ | |
runtimeConfig: { | ||
public: { | ||
hello: 'world', | ||
testValue: 'default' | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// TODO: export these | ||
// https://github.com/unjs/nitro/tree/main/src/runtime/utils.env.ts | ||
|
||
import destr from 'destr' | ||
import { snakeCase } from 'scule' | ||
|
||
export type EnvOptions = { | ||
prefix?: string | ||
altPrefix?: string | ||
} | ||
|
||
export function getEnv (key: string, opts: EnvOptions) { | ||
const envKey = snakeCase(key).toUpperCase() | ||
return destr( | ||
process.env[opts.prefix + envKey] ?? process.env[opts.altPrefix + envKey] | ||
) | ||
} | ||
|
||
function _isObject (input: unknown) { | ||
return typeof input === 'object' && !Array.isArray(input) | ||
} | ||
|
||
export function applyEnv (obj: Record<string, any>, opts: EnvOptions, parentKey = '') { | ||
for (const key in obj) { | ||
const subKey = parentKey ? `${parentKey}_${key}` : key | ||
const envValue = getEnv(subKey, opts) | ||
if (_isObject(obj[key])) { | ||
// Same as before | ||
if (_isObject(envValue)) { | ||
obj[key] = { ...obj[key], ...(envValue as object) } | ||
applyEnv(obj[key], opts, subKey) | ||
} | ||
// If envValue is undefined | ||
// Then proceed to nested properties | ||
else if (envValue === undefined) { | ||
applyEnv(obj[key], opts, subKey) | ||
} | ||
// If envValue is a primitive other than undefined | ||
// Then set objValue and ignore the nested properties | ||
else { | ||
obj[key] = envValue ?? obj[key] | ||
} | ||
} else { | ||
obj[key] = envValue ?? obj[key] | ||
} | ||
} | ||
return obj | ||
} |