@buka/nestjs-config
Version:
An easy to use nestjs config module
28 lines (27 loc) • 1.11 kB
JavaScript
import { Logger } from '@nestjs/common';
import dotenvx from '@dotenvx/dotenvx';
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 dotenvxLoader(filepath, loaderOptions = {}) {
const separator = loaderOptions.separator || '__';
const processEnv = loaderOptions.processEnv;
const privateKey = loaderOptions.privateKey;
return async (options) => {
if (!await fsExist(filepath)) {
if (!options.suppressWarnings) {
Logger.warn(`env file not found: ${filepath}`, '@buka/nestjs-config');
}
return {};
}
const content = await readFile(filepath);
const config = dotenvx.parse(content, { processEnv, privateKey });
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;
};
}