Skip to content
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: envFiles in netlify.toml #4321

Merged
merged 30 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3d7f48a
fix: npm run docs on Windows (#3973)
iib0011 Feb 16, 2022
8b6c546
feat: env_priority in netlify.toml (#4272)
iib0011 Feb 17, 2022
c8d207b
feat: dev --env flag (#4272)
iib0011 Feb 17, 2022
eb51778
Merge branch 'netlify:main' into envfix
iib0011 Feb 17, 2022
672903d
chore: rollback docs
iib0011 Feb 17, 2022
db6ca39
refactor: envPriority-> envFiles
iib0011 Feb 17, 2022
5dd6dc6
chore: comma separated list for env option
iib0011 Feb 17, 2022
316e367
chore: comma separated list for env option
iib0011 Feb 17, 2022
390f44a
Merge branch 'main' into envfix
erezrokah Feb 17, 2022
1064933
Merge branch 'main' into envfix
iib0011 Feb 17, 2022
3611233
chore: env flag removal
iib0011 Feb 18, 2022
ea28bb2
chore: env flag removal
iib0011 Feb 18, 2022
bd18271
Merge branch 'envfix' of https://github.com/iib0011/cli into envfix
iib0011 Feb 18, 2022
152b49f
chore: env files test and docs
iib0011 Feb 18, 2022
c86384a
chore: env_files test
iib0011 Feb 18, 2022
876a618
chore: env_files test
iib0011 Feb 18, 2022
132c6e6
chore: env_files test title
iib0011 Feb 18, 2022
27f1825
chore: env_files test fix
iib0011 Feb 18, 2022
fd07a35
Merge branch 'envfix' of https://github.com/iib0011/cli into envfix
iib0011 Feb 18, 2022
ce2f68f
refactor: env_files -> envFiles
iib0011 Feb 18, 2022
e849722
chore: env_files in dev block
iib0011 Feb 18, 2022
1a90081
Merge branch 'main' into envfix
erezrokah Feb 21, 2022
aae03ad
Merge branch 'main' into envfix
erezrokah Feb 21, 2022
3c295d8
fix(docs): move `envFiles` to the general section
erezrokah Feb 21, 2022
a239de2
chore: use `dev` block in test
erezrokah Feb 21, 2022
efde8de
Merge branch 'envfix' of https://github.com/iib0011/cli into envfix
iib0011 Feb 21, 2022
070ffb3
fix: env_files test
iib0011 Feb 21, 2022
97380fa
fix: apply config envFiles from left (highest) to right (lowest)
erezrokah Feb 22, 2022
12a29c4
Merge branch 'main' into envfix
erezrokah Feb 22, 2022
e9ca564
chore: remove redundant doc line
erezrokah Feb 22, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/netlify-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Netlify Dev is meant to work with zero config for the majority of users, by usin
jwtSecret = "secret" # The secret used to verify tokens for JWT based redirects
jwtRolePath = "app_metadata.authorization.roles" # Object path we should look for role values for JWT based redirects
autoLaunch = true # a Boolean value that determines if Netlify Dev launches the local server address in your browser
envFiles = [".env", ".env.development"] # The env files to use, ordered by priority
# to start an https server instead of an http one, configure a certificate and key files
[dev.https]
certFile = "cert.pem" # path to the certificate file
Expand Down
4 changes: 2 additions & 2 deletions src/commands/dev/dev-exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const { injectEnvVariables } = require('../../utils')
* @param {import('../base-command').BaseCommand} command
*/
const devExec = async (cmd, options, command) => {
const { cachedConfig, site } = command.netlify
await injectEnvVariables({ env: cachedConfig.env, site })
const { cachedConfig, config, site } = command.netlify
await injectEnvVariables({ devConfig: { ...config.dev }, env: cachedConfig.env, site })

await execa(cmd, command.args.slice(1), {
stdio: 'inherit',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/dev/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ const dev = async (options, command) => {
)
}

await injectEnvVariables({ env: command.netlify.cachedConfig.env, site })
await injectEnvVariables({ devConfig, env: command.netlify.cachedConfig.env, site })

const { addonsUrls, capabilities, siteUrl, timeouts } = await getSiteInformation({
// inherited from base command --offline
Expand Down
3 changes: 2 additions & 1 deletion src/commands/dev/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ export type DevConfig = {
https?: {
keyFile: string
certFile: string
}
},
envFiles?:string[]
}
16 changes: 14 additions & 2 deletions src/commands/functions/functions-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,14 @@ const createFunctionAddon = async function ({ addonName, addons, api, siteData,
* @param {(command: import('../base-command').BaseCommand) => any} config.onComplete
*/
const handleOnComplete = async ({ command, onComplete }) => {
const { config } = command.netlify

if (onComplete) {
await injectEnvVariables({ env: command.netlify.cachedConfig.env, site: command.netlify.site })
await injectEnvVariables({
devConfig: { ...config.dev },
env: command.netlify.cachedConfig.env,
site: command.netlify.site,
})
await onComplete.call(command)
}
}
Expand All @@ -491,6 +497,8 @@ const handleOnComplete = async ({ command, onComplete }) => {
* @param {string} config.fnPath
*/
const handleAddonDidInstall = async ({ addonCreated, addonDidInstall, command, fnPath }) => {
const { config } = command.netlify

if (!addonCreated || !addonDidInstall) {
return
}
Expand All @@ -508,7 +516,11 @@ const handleAddonDidInstall = async ({ addonCreated, addonDidInstall, command, f
return
}

await injectEnvVariables({ env: command.netlify.cachedConfig.env, site: command.netlify.site })
await injectEnvVariables({
devConfig: { ...config.dev },
env: command.netlify.cachedConfig.env,
site: command.netlify.site,
})
addonDidInstall(fnPath)
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/functions/functions-serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const functionsServe = async (options, command) => {

const functionsDir = getFunctionsDir({ options, config }, join('netlify', 'functions'))

await injectEnvVariables({ env: command.netlify.cachedConfig.env, site })
await injectEnvVariables({ devConfig: { ...config.dev }, env: command.netlify.cachedConfig.env, site })

const { capabilities, siteUrl, timeouts } = await getSiteInformation({
offline: options.offline,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ const getEnvSourceName = (source) => {

// 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`.
const injectEnvVariables = async ({ env, site }) => {
const injectEnvVariables = async ({ devConfig, env, site }) => {
const environment = new Map(Object.entries(env))
const dotEnvFiles = await loadDotEnvFiles({ projectDir: site.root })
const dotEnvFiles = await loadDotEnvFiles({ envFiles: devConfig.envFiles, projectDir: site.root })

dotEnvFiles.forEach(({ env: fileEnv, file }) => {
Object.keys(fileEnv).forEach((key) => {
Expand Down
9 changes: 5 additions & 4 deletions src/utils/dot-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const { isFileAsync } = require('../lib/fs')

const { warn } = require('./command-helpers')

const loadDotEnvFiles = async function ({ projectDir }) {
const response = await tryLoadDotEnvFiles({ projectDir })
const loadDotEnvFiles = async function ({ envFiles, projectDir }) {
const response = await tryLoadDotEnvFiles({ projectDir, dotenvFiles: envFiles })

const filesWithWarning = response.filter((el) => el.warning)
filesWithWarning.forEach((el) => {
Expand All @@ -19,8 +19,9 @@ const loadDotEnvFiles = async function ({ projectDir }) {
return response.filter((el) => el.file && el.env)
}

const tryLoadDotEnvFiles = async ({ projectDir }) => {
const dotenvFiles = ['.env', '.env.development']
const defaultEnvFiles = ['.env', '.env.development']

const tryLoadDotEnvFiles = async ({ projectDir, dotenvFiles = defaultEnvFiles }) => {
const results = await Promise.all(
dotenvFiles.map(async (file) => {
const filepath = path.resolve(projectDir, file)
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/300.command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,29 @@ testMatrix.forEach(({ args }) => {
})
})
})
test(testName('should use .env vars instead of .env.development vars', args), async (t) => {
await withSiteBuilder('site-with-env-files', async (builder) => {
builder
.withNetlifyToml({
config: { dev: { envFiles: ['.env.development', '.env'] }, functions: { directory: 'functions' } },
erezrokah marked this conversation as resolved.
Show resolved Hide resolved
})
.withEnvFile({ path: '.env.development', env: { TEST: 'FROM_ENV_DEV_FILE' } })
.withEnvFile({ path: '.env', env: { TEST: 'FROM_ENV_FILE' } })
.withFunction({
path: 'env.js',
handler: async () => ({
statusCode: 200,
body: `${process.env.TEST}`,
metadata: { builder_function: true },
}),
})

await builder.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async (server) => {
t.true(server.output.includes('Ignored .env file'))
})
})
})
})
/* eslint-enable require-await */