Skip to content

Commit

Permalink
feat(env): add .local variants to defaultEnvFiles (netlify#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctemplin committed Jul 27, 2022
1 parent 7b89caf commit 9d6104c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/netlify-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +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.development", ".env"] # The env files to use, ordered by priority (left - highest, right - lowest)
envFiles = [".env.development.local", ".env.local", ".env.development", ".env"] # The env files to use, ordered by priority (left - highest, right - lowest)
# 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
2 changes: 1 addition & 1 deletion src/utils/dot-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const loadDotEnvFiles = async function ({ envFiles, projectDir }) {
}

// in the user configuration, the order is highest to lowest
const defaultEnvFiles = ['.env.development', '.env']
const defaultEnvFiles = ['.env.development.local', '.env.local', '.env.development', '.env']

const tryLoadDotEnvFiles = async ({ projectDir, dotenvFiles = defaultEnvFiles }) => {
const results = await Promise.all(
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/utils/dot-env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,35 @@ test('should read env vars from .env.development file', async (t) => {
})
})

test('should read from both .env.development and .env', async (t) => {
test('should read env vars from .env.local file', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder.withEnvFile({
path: '.env.local',
env: { TEST: 'FROM_LOCAL_ENV' },
})
await builder.buildAsync()

const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [{ file: '.env.local', env: { TEST: 'FROM_LOCAL_ENV' } }])
})
})

test('should read env vars from .env.development.local file', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder.withEnvFile({
path: '.env.development.local',
env: { TEST: 'FROM_LOCAL_DEVELOPMENT_ENV' },
})
await builder.buildAsync()

const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [{ file: '.env.development.local', env: { TEST: 'FROM_LOCAL_DEVELOPMENT_ENV' } }])
})
})

test('should read env vars from all four .env[.development][.local] files', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder
Expand All @@ -54,12 +82,25 @@ test('should read from both .env.development and .env', async (t) => {
path: '.env.development',
env: { ONE: 'FROM_DEVELOPMENT_ENV', THREE: 'FROM_DEVELOPMENT_ENV' },
})
.withEnvFile({
path: '.env.local',
env: { ONE: 'FROM_LOCAL_ENV', FOUR: 'FROM_LOCAL_ENV' },
})
.withEnvFile({
path: '.env.development.local',
env: { ONE: 'FROM_LOCAL_DEVELOPMENT_ENV', FIVE: 'FROM_LOCAL_DEVELOPMENT_ENV' },
})
await builder.buildAsync()

const results = await tryLoadDotEnvFiles({ projectDir: builder.directory })
t.deepEqual(results, [
{ file: '.env', env: { ONE: 'FROM_ENV', TWO: 'FROM_ENV' } },
{ file: '.env.development', env: { ONE: 'FROM_DEVELOPMENT_ENV', THREE: 'FROM_DEVELOPMENT_ENV' } },
{ file: '.env.local', env: { ONE: 'FROM_LOCAL_ENV', FOUR: 'FROM_LOCAL_ENV' } },
{
file: '.env.development.local',
env: { ONE: 'FROM_LOCAL_DEVELOPMENT_ENV', FIVE: 'FROM_LOCAL_DEVELOPMENT_ENV' },
},
])
})
})
Expand Down

0 comments on commit 9d6104c

Please sign in to comment.