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.
Merge branch 'chore/vitepkg-dual' of github.com:dac09/redwood into ch…
…ore/vitepkg-dual * 'chore/vitepkg-dual' of github.com:dac09/redwood: fix(docs): Fix introduction link (redwoodjs#10938) fix(cli-helpers): Fix CJS support (redwoodjs#10936) chore(cli-helpers): Formatting chore(cli-helpers): loadEnvFiles cleanup (redwoodjs#10935) feat(cli-helpers): Add loadEnvFiles (redwoodjs#10931)
- Loading branch information
Showing
11 changed files
with
109 additions
and
14 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
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
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,71 @@ | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
|
||
import { config as dotenvConfig } from 'dotenv' | ||
import { config as dotenvDefaultsConfig } from 'dotenv-defaults' | ||
import { hideBin, Parser } from 'yargs/helpers' | ||
|
||
import { getPaths } from '@redwoodjs/project-config' | ||
|
||
export function loadEnvFiles() { | ||
if (process.env.REDWOOD_ENV_FILES_LOADED) { | ||
return | ||
} | ||
|
||
const { base } = getPaths() | ||
|
||
loadDefaultEnvFiles(base) | ||
loadNodeEnvDerivedEnvFile(base) | ||
|
||
const { loadEnvFiles } = Parser.default(hideBin(process.argv), { | ||
array: ['load-env-files'], | ||
default: { | ||
loadEnvFiles: [], | ||
}, | ||
}) | ||
|
||
if (loadEnvFiles.length > 0) { | ||
loadUserSpecifiedEnvFiles(base, loadEnvFiles) | ||
} | ||
|
||
process.env.REDWOOD_ENV_FILES_LOADED = 'true' | ||
} | ||
|
||
export function loadDefaultEnvFiles(cwd: string) { | ||
dotenvDefaultsConfig({ | ||
path: path.join(cwd, '.env'), | ||
defaults: path.join(cwd, '.env.defaults'), | ||
// @ts-expect-error - Old typings. @types/dotenv-defaults depends on dotenv | ||
// v8. dotenv-defaults uses dotenv v14 | ||
multiline: true, | ||
}) | ||
} | ||
|
||
export function loadNodeEnvDerivedEnvFile(cwd: string) { | ||
if (!process.env.NODE_ENV) { | ||
return | ||
} | ||
|
||
const nodeEnvDerivedEnvFilePath = path.join( | ||
cwd, | ||
`.env.${process.env.NODE_ENV}`, | ||
) | ||
if (!fs.existsSync(nodeEnvDerivedEnvFilePath)) { | ||
return | ||
} | ||
|
||
dotenvConfig({ path: nodeEnvDerivedEnvFilePath, override: true }) | ||
} | ||
|
||
export function loadUserSpecifiedEnvFiles(cwd: string, loadEnvFiles: string[]) { | ||
for (const suffix of loadEnvFiles) { | ||
const envPath = path.join(cwd, `.env.${suffix}`) | ||
if (!fs.existsSync(envPath)) { | ||
throw new Error( | ||
`Couldn't find an .env file at '${envPath}' as specified by '--load-env-files'`, | ||
) | ||
} | ||
|
||
dotenvConfig({ path: envPath, override: true }) | ||
} | ||
} |
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
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
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