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

fix(env files): rename to --load-env-files #10123

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ Some apps rely on reading the host header(eg multi-tenant apps served over multi

- Add support for loading more env var files (#9961, #10093, and #10094)

Fixes #9877. This PR adds CLI functionality to load more `.env` files via `NODE_ENV` and an `--add-env-files` flag.
Fixes #9877. This PR adds CLI functionality to load more `.env` files via `NODE_ENV` and an `--load-env-files` flag.
Env vars loaded via either of these methods override the values in `.env`:

```
# Loads '.env.production', which overrides values in '.env'
NODE_ENV=production yarn rw exec myScript

# Load '.env.stripe' and '.env.nakama', which overrides values
yarn rw exec myScript --add-env-files stripe --add-env-files nakama
# Or you can specify the flag once:
yarn rw exec myScript --add-env-files stripe nakama
yarn rw exec myScript --load-env-files stripe nakama
# Or you can specify them individually:
yarn rw exec myScript --load-env-files stripe --load-env-files nakama
```

Note that this feature is mainly for local scripting. Most deploy providers don't let you upload `.env` files (unless you're using baremetal) and usually have their own way of determining environments.
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async function runYargs() {
(argv) => {
delete argv.cwd
delete argv.addEnvFiles
delete argv['add-env-files']
delete argv['load-env-files']
delete argv.telemetry
},
telemetry && telemetryMiddleware,
Expand All @@ -176,13 +176,13 @@ async function runYargs() {
.option('cwd', {
describe: 'Working directory to use (where `redwood.toml` is located)',
})
.option('add-env-files', {
.option('load-env-files', {
describe:
'Load additional .env files. Values defined in files specified later override earlier ones.',
array: true,
})
.example(
'yarn rw exec migrateUsers --add-env-files stripe nakama',
'yarn rw exec migrateUsers --load-env-files stripe nakama',
"Run a script, also loading env vars from '.env.stripe' and '.env.nakama'"
)
.option('telemetry', {
Expand All @@ -192,7 +192,7 @@ async function runYargs() {
})
.example(
'yarn rw g page home /',
"\"Create a page component named 'Home' at path '/'\""
"Create a page component named 'Home' at path '/'"
)
.demandCommand()
.strict()
Expand Down
16 changes: 8 additions & 8 deletions packages/cli/src/lib/loadEnvFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export function loadEnvFiles() {
loadDefaultEnvFiles(base)
loadNodeEnvDerivedEnvFile(base)

const { addEnvFiles } = Parser(hideBin(process.argv), {
array: ['add-env-files'],
const { loadEnvFiles } = Parser(hideBin(process.argv), {
array: ['load-env-files'],
default: {
addEnvFiles: [],
loadEnvFiles: [],
},
})
if (addEnvFiles.length > 0) {
loadUserSpecifiedEnvFiles(base, addEnvFiles)
if (loadEnvFiles.length > 0) {
loadUserSpecifiedEnvFiles(base, loadEnvFiles)
}

process.env.REDWOOD_ENV_FILES_LOADED = 'true'
Expand Down Expand Up @@ -65,12 +65,12 @@ export function loadNodeEnvDerivedEnvFile(cwd) {
/**
* @param {string} cwd
*/
export function loadUserSpecifiedEnvFiles(cwd, addEnvFiles) {
for (const suffix of addEnvFiles) {
export function loadUserSpecifiedEnvFiles(cwd, loadEnvFiles) {
for (const suffix of loadEnvFiles) {
const envPath = path.join(cwd, `.env.${suffix}`)
if (!fs.pathExistsSync(envPath)) {
throw new Error(
`Couldn't find an .env file at '${envPath}' as specified by '--add-env-files'`
`Couldn't find an .env file at '${envPath}' as specified by '--load-env-files'`
)
}

Expand Down
4 changes: 2 additions & 2 deletions tasks/server-tests/bothServer.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('rw serve', () => {
--version Show version number [boolean]
--cwd Working directory to use (where
\`redwood.toml\` is located)
--add-env-files Load additional .env files. Values
--load-env-files Load additional .env files. Values
defined in files specified later
override earlier ones. [array]
--telemetry Whether to send anonymous usage
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('rw serve', () => {
--version Show version number [boolean]
--cwd Working directory to use (where
\`redwood.toml\` is located)
--add-env-files Load additional .env files. Values
--load-env-files Load additional .env files. Values
defined in files specified later
override earlier ones. [array]
--telemetry Whether to send anonymous usage
Expand Down
Loading