From 2a62c3683c8b02bd075dd774d1e814736853f93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Alvergnat?= Date: Thu, 17 Mar 2022 12:06:14 +0100 Subject: [PATCH] feat(expand): add dotenv-expand options support --- lib/config.module.ts | 5 ++-- .../config-module-options.interface.ts | 6 ++-- ...ad-env-expanded-ignore-process-env.spec.ts | 28 +++++++++++++++++++ tests/src/app.module.ts | 12 ++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 tests/e2e/load-env-expanded-ignore-process-env.spec.ts diff --git a/lib/config.module.ts b/lib/config.module.ts index e9d3239c..b912d101 100644 --- a/lib/config.module.ts +++ b/lib/config.module.ts @@ -2,7 +2,7 @@ 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 { expand } from 'dotenv-expand'; +import { DotenvExpandOptions, expand } from 'dotenv-expand'; import * as fs from 'fs'; import { resolve } from 'path'; import { ConfigHostModule } from './config-host.module'; @@ -184,7 +184,8 @@ export class ConfigModule { config, ); if (options.expandVariables) { - config = expand({ parsed: config }).parsed || config; + const expandOptions: DotenvExpandOptions = typeof options.expandVariables === 'object' ? options.expandVariables : {}; + config = expand({ ...expandOptions, parsed: config }).parsed || config; } } } diff --git a/lib/interfaces/config-module-options.interface.ts b/lib/interfaces/config-module-options.interface.ts index 2e610763..7c0fccc0 100644 --- a/lib/interfaces/config-module-options.interface.ts +++ b/lib/interfaces/config-module-options.interface.ts @@ -1,4 +1,5 @@ import { ConfigFactory } from './config-factory.interface'; +import { DotenvExpandOptions } from 'dotenv-expand' export interface ConfigModuleOptions { /** @@ -61,9 +62,10 @@ export interface ConfigModuleOptions { load?: Array; /** - * A boolean value indicating the use of expanded variables. + * A boolean value indicating the use of expanded variables, or object + * containing options to pass to dotenv-expand. * If .env contains expanded variables, they'll only be parsed if * this property is set to true. */ - expandVariables?: boolean; + expandVariables?: boolean | DotenvExpandOptions; } diff --git a/tests/e2e/load-env-expanded-ignore-process-env.spec.ts b/tests/e2e/load-env-expanded-ignore-process-env.spec.ts new file mode 100644 index 00000000..f246bde9 --- /dev/null +++ b/tests/e2e/load-env-expanded-ignore-process-env.spec.ts @@ -0,0 +1,28 @@ +import { INestApplication } from '@nestjs/common'; +import { Test } from '@nestjs/testing'; +import { AppModule } from '../src/app.module'; + +describe('Environment variables', () => { + let app: INestApplication; + + beforeEach(async () => { + process.env.URL = 'process-app.test'; + + const module = await Test.createTestingModule({ + imports: [AppModule.withExpandedEnvVarsIgnoreProcessEnv()], + }).compile(); + + app = module.createNestApplication(); + await app.init(); + }); + + it(`should ignore process environment variable`, () => { + const envVars = app.get(AppModule).getEnvVariables(); + expect(envVars.EMAIL).toEqual('support@myapp.test'); + }); + + afterEach(async () => { + process.env.URL = undefined + await app.close(); + }); +}); diff --git a/tests/src/app.module.ts b/tests/src/app.module.ts index 502b2fbe..82df44c4 100644 --- a/tests/src/app.module.ts +++ b/tests/src/app.module.ts @@ -92,6 +92,18 @@ export class AppModule { }; } + static withExpandedEnvVarsIgnoreProcessEnv(): DynamicModule { + return { + module: AppModule, + imports: [ + ConfigModule.forRoot({ + envFilePath: join(__dirname, '.env.expanded'), + expandVariables: { ignoreProcessEnv: true } + }), + ], + }; + } + static withMultipleEnvFiles(): DynamicModule { return { module: AppModule,