| |
| import * as fs from 'fs' |
| import * as path from 'path' |
| import * as dotenv from 'dotenv' |
| import { expand as dotenvExpand } from 'dotenv-expand' |
|
|
| export type Env = { [key: string]: string | undefined } |
| export type LoadedEnvFiles = Array<{ |
| path: string |
| contents: string |
| env: Env |
| }> |
|
|
| export let initialEnv: Env | undefined = undefined |
| let combinedEnv: Env | undefined = undefined |
| let parsedEnv: Env | undefined = undefined |
| let cachedLoadedEnvFiles: LoadedEnvFiles = [] |
| let previousLoadedEnvFiles: LoadedEnvFiles = [] |
|
|
| export function updateInitialEnv(newEnv: Env) { |
| Object.assign(initialEnv || {}, newEnv) |
| } |
|
|
| type Log = { |
| info: (...args: any[]) => void |
| error: (...args: any[]) => void |
| } |
|
|
| function replaceProcessEnv(sourceEnv: Env) { |
| Object.keys(process.env).forEach((key) => { |
| |
| |
| if (!key.startsWith('__NEXT_PRIVATE')) { |
| if (sourceEnv[key] === undefined || sourceEnv[key] === '') { |
| delete process.env[key] |
| } |
| } |
| }) |
|
|
| Object.entries(sourceEnv).forEach(([key, value]) => { |
| process.env[key] = value |
| }) |
| } |
|
|
| export function processEnv( |
| loadedEnvFiles: LoadedEnvFiles, |
| dir?: string, |
| log: Log = console, |
| forceReload = false, |
| onReload?: (envFilePath: string) => void |
| ) { |
| if (!initialEnv) { |
| initialEnv = Object.assign({}, process.env) |
| } |
| |
| if ( |
| !forceReload && |
| (process.env.__NEXT_PROCESSED_ENV || loadedEnvFiles.length === 0) |
| ) { |
| return [process.env as Env] |
| } |
| |
| process.env.__NEXT_PROCESSED_ENV = 'true' |
|
|
| const origEnv = Object.assign({}, initialEnv) |
| const parsed: dotenv.DotenvParseOutput = {} |
|
|
| for (const envFile of loadedEnvFiles) { |
| try { |
| let result: dotenv.DotenvConfigOutput = {} |
| result.parsed = dotenv.parse(envFile.contents) |
|
|
| result = dotenvExpand(result) |
|
|
| if ( |
| result.parsed && |
| !previousLoadedEnvFiles.some( |
| (item) => |
| item.contents === envFile.contents && item.path === envFile.path |
| ) |
| ) { |
| onReload?.(envFile.path) |
| } |
|
|
| for (const key of Object.keys(result.parsed || {})) { |
| if ( |
| typeof parsed[key] === 'undefined' && |
| typeof origEnv[key] === 'undefined' |
| ) { |
| |
| |
| parsed[key] = result.parsed?.[key]! |
| } |
| } |
|
|
| |
| envFile.env = result.parsed || {} |
| } catch (err) { |
| log.error( |
| `Failed to load env from ${path.join(dir || '', envFile.path)}`, |
| err |
| ) |
| } |
| } |
| return [Object.assign(process.env, parsed), parsed] |
| } |
|
|
| export function resetEnv() { |
| if (initialEnv) { |
| replaceProcessEnv(initialEnv) |
| } |
| } |
|
|
| export function loadEnvConfig( |
| dir: string, |
| dev?: boolean, |
| log: Log = console, |
| forceReload = false, |
| onReload?: (envFilePath: string) => void |
| ): { |
| combinedEnv: Env |
| parsedEnv: Env | undefined |
| loadedEnvFiles: LoadedEnvFiles |
| } { |
| if (!initialEnv) { |
| initialEnv = Object.assign({}, process.env) |
| } |
| |
| if (combinedEnv && !forceReload) { |
| return { combinedEnv, parsedEnv, loadedEnvFiles: cachedLoadedEnvFiles } |
| } |
| replaceProcessEnv(initialEnv) |
| previousLoadedEnvFiles = cachedLoadedEnvFiles |
| cachedLoadedEnvFiles = [] |
|
|
| const isTest = process.env.NODE_ENV === 'test' |
| const mode = isTest ? 'test' : dev ? 'development' : 'production' |
| const dotenvFiles = [ |
| `.env.${mode}.local`, |
| |
| |
| |
| mode !== 'test' && `.env.local`, |
| `.env.${mode}`, |
| '.env', |
| ].filter(Boolean) as string[] |
|
|
| for (const envFile of dotenvFiles) { |
| |
| const dotEnvPath = path.join(dir, envFile) |
|
|
| try { |
| const stats = fs.statSync(dotEnvPath) |
|
|
| |
| if (!stats.isFile() && !stats.isFIFO()) { |
| continue |
| } |
|
|
| const contents = fs.readFileSync(dotEnvPath, 'utf8') |
| cachedLoadedEnvFiles.push({ |
| path: envFile, |
| contents, |
| env: {}, |
| }) |
| } catch (err: any) { |
| if (err.code !== 'ENOENT') { |
| log.error(`Failed to load env from ${envFile}`, err) |
| } |
| } |
| } |
| ;[combinedEnv, parsedEnv] = processEnv( |
| cachedLoadedEnvFiles, |
| dir, |
| log, |
| forceReload, |
| onReload |
| ) |
| return { combinedEnv, parsedEnv, loadedEnvFiles: cachedLoadedEnvFiles } |
| } |
|
|