From 9d6104c63d71a21e6e9fec6b56f7ff671d615b86 Mon Sep 17 00:00:00 2001 From: Chris Templin Date: Wed, 27 Jul 2022 14:23:19 -0400 Subject: [PATCH] feat(env): add .local variants to defaultEnvFiles (#1604) --- docs/netlify-dev.md | 2 +- src/utils/dot-env.js | 2 +- tests/unit/utils/dot-env.test.js | 43 +++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/docs/netlify-dev.md b/docs/netlify-dev.md index 504f5ccc18e..f67b87be870 100644 --- a/docs/netlify-dev.md +++ b/docs/netlify-dev.md @@ -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 diff --git a/src/utils/dot-env.js b/src/utils/dot-env.js index ee5efc8f064..3e50f481fb3 100644 --- a/src/utils/dot-env.js +++ b/src/utils/dot-env.js @@ -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( diff --git a/tests/unit/utils/dot-env.test.js b/tests/unit/utils/dot-env.test.js index b93bc8a6728..a091849f18b 100644 --- a/tests/unit/utils/dot-env.test.js +++ b/tests/unit/utils/dot-env.test.js @@ -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 @@ -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' }, + }, ]) }) })