Skip to content

Commit

Permalink
Add support for additional .env files in the cli = re: redwoodjs#9877
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Feb 3, 2024
1 parent b930777 commit ec36eeb
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import * as upgradeCommand from './commands/upgrade'
import { getPaths, findUp } from './lib'
import { exitWithError } from './lib/exit'
import * as updateCheck from './lib/updateCheck'
import { addAdditionalEnvFiles } from './middleware/addAdditionalEnvFiles'
import { loadPlugins } from './plugin'
import { startTelemetry, shutdownTelemetry } from './telemetry/index'

Expand Down Expand Up @@ -104,6 +105,7 @@ process.env.RWJS_CWD = cwd
// # Load .env, .env.defaults
//
// This should be done as early as possible, and the earliest we can do it is after setting `cwd`.
// Further down in middleware, we allow additional .env files to be loaded based on args.

if (!process.env.REDWOOD_ENV_FILES_LOADED) {
config({
Expand Down Expand Up @@ -181,6 +183,30 @@ async function runYargs() {
.option('cwd', {
describe: 'Working directory to use (where `redwood.toml` is located)',
})
.option('include-env', {
describe:
'Add include running additional environment files to your command. These are incremental',
array: true,
})
.example(
'rw exec MigrateUsers --include-env prod --include-env stripe-prod',
'run a script, and also include .env.prod and .env.stripe-prod'
)
.middleware(
[
// We've already handled `cwd` above, but it may still be in `argv`.
// We don't need it anymore so let's get rid of it.
// Likewise for `telemetry`.
(argv) => {
delete argv.cwd
delete argv.telemetry
},
telemetry && telemetryMiddleware,
updateCheck.isEnabled() && updateCheck.updateCheckMiddleware,
addAdditionalEnvFiles(cwd),
].filter(Boolean)
)

.option('telemetry', {
describe: 'Whether to send anonymous usage telemetry to RedwoodJS',
boolean: true,
Expand Down
32 changes: 32 additions & 0 deletions packages/cli/src/middleware/addAdditionalEnvFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @ts-check
import { existsSync } from 'fs'

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 ('includeEnv' in yargs && Array.isArray(yargs.includeEnv)) {
for (const suffix of yargs.includeEnv) {
const envPath = `${cwd}/.env.${suffix}`
if (!existsSync(envPath)) {
throw new Error(
`Couldn't find an .env file at '${envPath}' - which was noted via --include-env`
)
}

config({ path: `${cwd}/.env.${suffix}` })
}
}

// Support automatically matching a .env file based on the NODE_ENV
if (process.env.NODE_ENV) {
const processBasedEnvPath = `${cwd}/.env.${process.env.NODE_ENV}`
if (existsSync(processBasedEnvPath)) {
config({ path: processBasedEnvPath })
}
}
}

0 comments on commit ec36eeb

Please sign in to comment.