UNPKG

@buka/nestjs-config

Version:
26 lines (25 loc) 979 B
import { Logger } from '@nestjs/common'; import dotenv from 'dotenv'; import { readFile } from 'fs/promises'; import * as R from 'ramda'; import { fsExist } from '../utils/fs-exists.js'; import { parseValue } from "../utils/parse-value.js"; export function dotenvLoader(filepath, loaderOptions = {}) { const separator = loaderOptions.separator || '__'; return async (moduleOptions) => { if (!await fsExist(filepath)) { if (!moduleOptions.suppressWarnings) { Logger.warn(`env file not found: ${filepath}`, '@buka/nestjs-config'); } return {}; } const content = await readFile(filepath); const config = dotenv.parse(content); let result = {}; for (const key of Object.keys(config)) { const value = parseValue(config[key], loaderOptions.jsonParse); result = R.assocPath(key.split(separator), value, result); } return result; }; }