Skip to content

Commit

Permalink
feat(): add env variables loaded public hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Feb 11, 2022
1 parent 579e173 commit b529e82
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 20 additions & 2 deletions lib/config.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DynamicModule, Module } from '@nestjs/common';
import { FactoryProvider } from '@nestjs/common/interfaces';
import { isObject } from '@nestjs/common/utils/shared.utils';
import * as dotenv from 'dotenv';
import dotenvExpand from 'dotenv-expand';
import * as fs from 'fs';
import { resolve } from 'path';
import { isObject } from 'util';
import { ConfigHostModule } from './config-host.module';
import {
CONFIGURATION_LOADER,
Expand All @@ -31,6 +31,20 @@ import { mergeConfigObject } from './utils/merge-configs.util';
exports: [ConfigHostModule, ConfigService],
})
export class ConfigModule {
/**
* This promise resolves when "dotenv" completes loading environment variables.
* When "ignoreEnvFile" is set to true, then it will resolve immediately after the
* "ConfigModule#forRoot" method is called.
*/
public static get envVariablesLoaded() {
return this._envVariablesLoaded;
}

private static environmentVariablesLoadedSignal: () => void;
private static readonly _envVariablesLoaded = new Promise<void>(
resolve => (ConfigModule.environmentVariablesLoadedSignal = resolve),
);

/**
* Loads process environment variables depending on the "ignoreEnvFile" flag and "envFilePath" value.
* Also, registers custom configurations globally.
Expand Down Expand Up @@ -95,6 +109,8 @@ export class ConfigModule {
providers.push(validatedEnvConfigLoader);
}

this.environmentVariablesLoadedSignal();

return {
module: ConfigModule,
global: options.isGlobal,
Expand Down Expand Up @@ -180,7 +196,9 @@ export class ConfigModule {
return;
}
const keys = Object.keys(config).filter(key => !(key in process.env));
keys.forEach(key => (process.env[key] = config[key]));
keys.forEach(
key => (process.env[key] = (config as Record<string, any>)[key]),
);
}

private static mergePartial(
Expand Down
5 changes: 4 additions & 1 deletion tests/e2e/load-env.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { ConfigModule } from '../../lib';
import { AppModule } from '../src/app.module';

describe('Environment variables', () => {
Expand All @@ -14,7 +15,9 @@ describe('Environment variables', () => {
await app.init();
});

it(`should return loaded env variables`, () => {
it(`should return loaded env variables`, async () => {
await ConfigModule.envVariablesLoaded;

const envVars = app.get(AppModule).getEnvVariables();
expect(envVars.PORT).toEqual('4000');
});
Expand Down

0 comments on commit b529e82

Please sign in to comment.