forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
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 files in the cli = re: redwoodjs#9877
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 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 |
---|---|---|
@@ -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 }) | ||
} | ||
} | ||
} |