Skip to content

Instantly share code, notes, and snippets.

@itsRems
Created February 19, 2022 01:49
Show Gist options
  • Save itsRems/f25635f20845dc09fb1d13a653965303 to your computer and use it in GitHub Desktop.
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
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;
}
@itsRems
Copy link
Author

itsRems commented Feb 19, 2022

with help from @Maxime9812

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment