-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for additional env var files (#9961)
Fixes #9877 Adds a new middleware step to the CLI booting up, which looks for `--include-env` and includes `.env.[file]` to the list of files to look at. Also generally adds a .env var based on the `NODE_ENV` - I could take this or leave it, but I was in the space. ![image](https://github.com/redwoodjs/redwood/assets/49038/267c9f8d-0b4d-4c83-8607-712b837e3cfc) --------- Co-authored-by: Tobbe Lundberg <tobbe@tlundberg.com> Co-authored-by: Dominic Saadi <dominiceliassaadi@gmail.com>
- Loading branch information
1 parent
6575687
commit 0ed2afe
Showing
16 changed files
with
242 additions
and
4 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
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
.idea | ||
.DS_Store | ||
.env | ||
.env* | ||
!.env.example | ||
!.env.defaults | ||
.netlify | ||
.redwood/* | ||
!.redwood/README.md | ||
|
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
.idea | ||
.DS_Store | ||
.env | ||
.env* | ||
!.env.example | ||
!.env.defaults | ||
.netlify | ||
.redwood/* | ||
!.redwood/README.md | ||
|
2 changes: 2 additions & 0 deletions
2
packages/cli/src/__tests__/__fixtures__/redwood-app-env-collision/.env.base
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,2 @@ | ||
DATABASE_URL="postgresql://user:password@localhost:5432/mydb" | ||
TEST_BASE=1 |
2 changes: 2 additions & 0 deletions
2
packages/cli/src/__tests__/__fixtures__/redwood-app-env-collision/.env.collision
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,2 @@ | ||
DATABASE_URL="postgresql://user:password@localhost:5432/mycollisiondb" | ||
TEST_COLLISION=1 |
1 change: 1 addition & 0 deletions
1
packages/cli/src/__tests__/__fixtures__/redwood-app-env-many/.env.dev
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 @@ | ||
DEV_DATABASE_URL="postgresql://user:password@localhost:5432/mydevdb" |
1 change: 1 addition & 0 deletions
1
packages/cli/src/__tests__/__fixtures__/redwood-app-env-many/.env.prod
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 @@ | ||
PROD_DATABASE_URL="postgresql://user:password@localhost:5432/myproddb" |
2 changes: 2 additions & 0 deletions
2
packages/cli/src/__tests__/__fixtures__/redwood-app-env-node-env/.env.bazinga
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,2 @@ | ||
PROD_DATABASE_URL="postgresql://user:password@localhost:5432/bazinga" | ||
BAZINGA=1 |
1 change: 1 addition & 0 deletions
1
packages/cli/src/__tests__/__fixtures__/redwood-app-env-node-env/.env.prod
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 @@ | ||
PROD_DATABASE_URL="postgresql://user:password@localhost:5432/myproddb" |
1 change: 1 addition & 0 deletions
1
packages/cli/src/__tests__/__fixtures__/redwood-app-env-prod/.env.prod
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 @@ | ||
PROD_DATABASE_URL="postgresql://user:password@localhost:5432/myproddb" |
137 changes: 137 additions & 0 deletions
137
packages/cli/src/__tests__/addAdditionalEnvFiles.test.js
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,137 @@ | ||
import path from 'path' | ||
|
||
import { afterEach, beforeAll, describe, expect, it, test } from 'vitest' | ||
|
||
import { addAdditionalEnvFiles } from '../middleware/addAdditionalEnvFiles' | ||
|
||
describe('addAdditionalEnvFiles', () => { | ||
let originalProcessEnv | ||
beforeAll(() => { | ||
originalProcessEnv = { ...process.env } | ||
}) | ||
afterEach(() => { | ||
process.env = { ...originalProcessEnv } | ||
}) | ||
|
||
it("doesn't load .env files if there are none to load", () => { | ||
const fn = addAdditionalEnvFiles(__dirname) | ||
fn({}) | ||
|
||
expect(process.env).toEqual(originalProcessEnv) | ||
}) | ||
|
||
it("doesn't load .env files if not instructed to", () => { | ||
const fn = addAdditionalEnvFiles( | ||
path.join(__dirname, '__fixtures__/redwood-app-env-prod') | ||
) | ||
fn({}) | ||
|
||
expect(process.env).toEqual(originalProcessEnv) | ||
}) | ||
|
||
it('loads specified .env files', () => { | ||
expect(process.env).not.toHaveProperty('PROD_DATABASE_URL') | ||
|
||
const fn = addAdditionalEnvFiles( | ||
path.join(__dirname, '__fixtures__/redwood-app-env-prod') | ||
) | ||
fn({ includeEnvFiles: ['prod'] }) | ||
|
||
expect(process.env).toHaveProperty( | ||
'PROD_DATABASE_URL', | ||
'postgresql://user:password@localhost:5432/myproddb' | ||
) | ||
}) | ||
|
||
test('process.env is reset between tests', () => { | ||
expect(process.env).not.toHaveProperty('PROD_DATABASE_URL') | ||
}) | ||
|
||
it('loads multiple .env files', () => { | ||
expect(process.env).not.toHaveProperty('DEV_DATABASE_URL') | ||
expect(process.env).not.toHaveProperty('PROD_DATABASE_URL') | ||
|
||
const fn = addAdditionalEnvFiles( | ||
path.join(__dirname, '__fixtures__/redwood-app-env-many') | ||
) | ||
fn({ includeEnvFiles: ['dev', 'prod'] }) | ||
|
||
expect(process.env).toHaveProperty( | ||
'DEV_DATABASE_URL', | ||
'postgresql://user:password@localhost:5432/mydevdb' | ||
) | ||
expect(process.env).toHaveProperty( | ||
'PROD_DATABASE_URL', | ||
'postgresql://user:password@localhost:5432/myproddb' | ||
) | ||
}) | ||
|
||
it('is additive (i.e. only adds to process.env, not overwrites)', () => { | ||
expect(process.env).not.toHaveProperty('DATABASE_URL') | ||
expect(process.env).not.toHaveProperty('TEST_BASE') | ||
expect(process.env).not.toHaveProperty('TEST_COLLISION') | ||
|
||
const fn = addAdditionalEnvFiles( | ||
path.join(__dirname, '__fixtures__/redwood-app-env-collision') | ||
) | ||
fn({ includeEnvFiles: ['base', 'collision'] }) | ||
|
||
expect(process.env).toHaveProperty( | ||
'DATABASE_URL', | ||
'postgresql://user:password@localhost:5432/mydb' | ||
) | ||
expect(process.env).toHaveProperty('TEST_BASE', '1') | ||
expect(process.env).toHaveProperty('TEST_COLLISION', '1') | ||
}) | ||
|
||
it('loads .env files based on NODE_ENV ', () => { | ||
expect(process.env).not.toHaveProperty('PROD_DATABASE_URL') | ||
expect(process.env).not.toHaveProperty('BAZINGA') | ||
|
||
process.env.NODE_ENV = 'bazinga' | ||
const fn = addAdditionalEnvFiles( | ||
path.join(__dirname, '__fixtures__/redwood-app-env-node-env') | ||
) | ||
fn({}) | ||
|
||
expect(process.env).toHaveProperty( | ||
'PROD_DATABASE_URL', | ||
'postgresql://user:password@localhost:5432/bazinga' | ||
) | ||
expect(process.env).toHaveProperty('BAZINGA', '1') | ||
}) | ||
|
||
it('loads .env files based on NODE_ENV last', () => { | ||
expect(process.env).not.toHaveProperty('PROD_DATABASE_URL') | ||
expect(process.env).not.toHaveProperty('BAZINGA') | ||
|
||
process.env.NODE_ENV = 'bazinga' | ||
const fn = addAdditionalEnvFiles( | ||
path.join(__dirname, '__fixtures__/redwood-app-env-node-env') | ||
) | ||
fn({ | ||
includeEnvFiles: ['prod'], | ||
}) | ||
|
||
expect(process.env).toHaveProperty( | ||
'PROD_DATABASE_URL', | ||
'postgresql://user:password@localhost:5432/myproddb' | ||
) | ||
expect(process.env).toHaveProperty('BAZINGA', '1') | ||
}) | ||
|
||
it("throws if it can't find a specified env file", () => { | ||
const fn = addAdditionalEnvFiles( | ||
path.join(__dirname, '__fixtures__/redwood-app-env-node-env') | ||
) | ||
|
||
try { | ||
fn({ | ||
includeEnvFiles: ['missing'], | ||
}) | ||
} catch (error) { | ||
// Just testing that the error message reports the file it tried to load. | ||
expect(error.message).toMatch(/\.env\.missing/) | ||
} | ||
}) | ||
}) |
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,33 @@ | ||
// @ts-check | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
import { config } from 'dotenv' | ||
|
||
/** | ||
* @param { string } cwd | ||
* @returns {(yargs: import('yargs').Argv) => void} | ||
*/ | ||
export const addAdditionalEnvFiles = (cwd) => (yargs) => { | ||
// Allow for additional .env files to be included via --include-env | ||
if ('includeEnvFiles' in yargs && Array.isArray(yargs.includeEnvFiles)) { | ||
for (const suffix of yargs.includeEnvFiles) { | ||
const envPath = path.join(cwd, `.env.${suffix}`) | ||
if (!fs.existsSync(envPath)) { | ||
throw new Error( | ||
`Couldn't find an .env file at '${envPath}' - which was noted via --include-env` | ||
) | ||
} | ||
|
||
config({ path: envPath }) | ||
} | ||
} | ||
|
||
// Support automatically matching a .env file based on NODE_ENV | ||
if (process.env.NODE_ENV) { | ||
const processBasedEnvPath = path.join(cwd, `.env.${process.env.NODE_ENV}`) | ||
if (fs.existsSync(processBasedEnvPath)) { | ||
config({ path: processBasedEnvPath }) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
.idea | ||
.DS_Store | ||
.env | ||
.env* | ||
!.env.example | ||
!.env.defaults | ||
.netlify | ||
.redwood/* | ||
!.redwood/README.md | ||
|
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
.idea | ||
.DS_Store | ||
.env | ||
.env* | ||
!.env.example | ||
!.env.defaults | ||
.netlify | ||
.redwood/* | ||
!.redwood/README.md | ||
|
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