-
Notifications
You must be signed in to change notification settings - Fork 365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: inject environment variables from .env files into edge functions locally #5620
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2dea9b5
feat: inject environment variables from .env files into edge function…
danez 5940f0e
chore: test
danez 84d4c24
chore: rename function
danez 2e6bc5a
Merge branch 'main' into env-edge
danez ac4f55f
Merge branch 'main' into env-edge
danez 597839d
chore: fix test on windows
danez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
|
@@ -137,30 +137,40 @@ const getEnvSourceName = (source) => { | |
return printFn(name) | ||
} | ||
|
||
// Takes a set of environment variables in the format provided by @netlify/config, augments it with variables from both | ||
// dot-env files and the process itself, and injects into `process.env`. | ||
export const injectEnvVariables = async ({ devConfig, env, site }) => { | ||
const environment = new Map(Object.entries(env)) | ||
/** | ||
* @param {{devConfig: any, env: Record<string, { sources: string[], value: string}>, site: any}} param0 | ||
* @returns {Promise<Record<string, { sources: string[], value: string}>>} | ||
*/ | ||
export const getDotEnvVariables = async ({ devConfig, env, site }) => { | ||
const dotEnvFiles = await loadDotEnvFiles({ envFiles: devConfig.envFiles, projectDir: site.root }) | ||
|
||
dotEnvFiles.forEach(({ env: fileEnv, file }) => { | ||
const newSourceName = `${file} file` | ||
|
||
Object.keys(fileEnv).forEach((key) => { | ||
const newSourceName = `${file} file` | ||
const sources = environment.has(key) ? [newSourceName, ...environment.get(key).sources] : [newSourceName] | ||
const sources = key in env ? [newSourceName, ...env[key].sources] : [newSourceName] | ||
|
||
if (sources.includes('internal')) { | ||
return | ||
} | ||
|
||
environment.set(key, { | ||
env[key] = { | ||
sources, | ||
value: fileEnv[key], | ||
}) | ||
} | ||
}) | ||
}) | ||
|
||
return env | ||
} | ||
|
||
/** | ||
* Takes a set of environment variables in the format provided by @netlify/config and injects them into `process.env` | ||
* @param {Record<string, { sources: string[], value: string}>} env | ||
* @return {void} | ||
*/ | ||
export const injectEnvVariables = (env) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this function to only inject variables into |
||
// eslint-disable-next-line fp/no-loops | ||
for (const [key, variable] of environment) { | ||
for (const [key, variable] of Object.entries(env)) { | ||
const existsInProcess = process.env[key] !== undefined | ||
const [usedSource, ...overriddenSources] = existsInProcess ? ['process', ...variable.sources] : variable.sources | ||
const usedSourceName = getEnvSourceName(usedSource) | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addDotEnvVariables
now mutatesenv
here and adds the variables from the dot-env files to it, so that later one we can use them in edge-functions. Previously dotenv variables were only added toprocess.env
directly.