@buka/nestjs-config
Version:
An easy to use nestjs config module
26 lines (25 loc) • 979 B
JavaScript
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;
};
}