-
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.
[chore] Re-use loadEnvFiles from cli-helpers lib, elimininating code …
…duplicatation (#11885)
- Loading branch information
Showing
11 changed files
with
21 additions
and
79 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,79 +1,16 @@ | ||
// @ts-check | ||
|
||
import path from 'path' | ||
|
||
import { config as dotenvConfig } from 'dotenv' | ||
import { config as dotenvDefaultsConfig } from 'dotenv-defaults' | ||
import fs from 'fs-extra' | ||
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(hideBin(process.argv), { | ||
array: ['load-env-files'], | ||
default: { | ||
loadEnvFiles: [], | ||
}, | ||
}) | ||
if (loadEnvFiles.length > 0) { | ||
loadUserSpecifiedEnvFiles(base, loadEnvFiles) | ||
} | ||
|
||
process.env.REDWOOD_ENV_FILES_LOADED = 'true' | ||
} | ||
|
||
/** | ||
* @param {string} cwd | ||
*/ | ||
export function loadDefaultEnvFiles(cwd) { | ||
dotenvDefaultsConfig({ | ||
path: path.join(cwd, '.env'), | ||
defaults: path.join(cwd, '.env.defaults'), | ||
multiline: true, | ||
}) | ||
} | ||
|
||
/** | ||
* @param {string} cwd | ||
*/ | ||
export function loadNodeEnvDerivedEnvFile(cwd) { | ||
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 { | ||
loadEnvFiles, | ||
loadDefaultEnvFiles, | ||
loadNodeEnvDerivedEnvFile, | ||
loadUserSpecifiedEnvFiles, | ||
} from '@redwoodjs/cli-helpers' | ||
|
||
/** | ||
* @param {string} cwd | ||
* Retaining the file here as per https://github.com/redwoodjs/redwood/pull/11885#issuecomment-2575847407 | ||
* | ||
* This ensures there are no breaking changes in case people are deep-importing any of these methods. | ||
* | ||
* This file can finally be removed in the next major version as a documented breaking change. | ||
*/ | ||
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 '--load-env-files'`, | ||
) | ||
} | ||
|
||
dotenvConfig({ path: envPath, override: true }) | ||
} | ||
} |