Created
February 19, 2022 01:49
-
-
Save itsRems/f25635f20845dc09fb1d13a653965303 to your computer and use it in GitHub Desktop.
Simple, shitty way of extracting .env's in nested folders, in ex. inside monorepos
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
import { existsSync, readFileSync } from "fs"; | |
import { join } from "path"; | |
export const production = process.env.NODE_ENV === 'production'; | |
const envMap: { | |
[key: string]: Record<string, string> | |
} = {}; | |
function getEnv (dirname: string, maxIteration: number = 10, currentIteration: number = 0): Record<string, string> { | |
const cached = envMap[dirname]; | |
if (cached && typeof cached === 'object') return cached; | |
if (currentIteration >= maxIteration) return; | |
const filePath = join(`${dirname}/${new Array(currentIteration).fill(0).map(i => '../').join('')}.env`); | |
if (!existsSync(filePath)) return getEnv(dirname, maxIteration, currentIteration + 1); | |
const file = readFileSync(filePath).toString(); | |
if (file.toUpperCase().includes('X-EXTRACTENV-IGNORE')) return getEnv(dirname, maxIteration, currentIteration + 1); | |
const env = {}; | |
for (const line of file.split('\n')) { | |
const [key, ...rest] = line.split('='); | |
env[key] = rest.join('=').toString(); | |
} | |
envMap[dirname] = env; | |
return env; | |
} | |
export function extractEnv (key: string, defaultValue?: string, dirname: string = __dirname) { | |
if (production) return process.env[key] ?? defaultValue; | |
const env = getEnv(dirname); | |
return env[key] ?? defaultValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
with help from @Maxime9812