Skip to content

Commit

Permalink
Merge pull request #891 from Toilal/dotenv-expand-options
Browse files Browse the repository at this point in the history
feat(expand): add dotenv-expand options support
  • Loading branch information
kamilmysliwiec committed Mar 17, 2022
2 parents 4df1c30 + 2a62c36 commit b725805
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/config.module.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions 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 {
/**
Expand Down Expand Up @@ -61,9 +62,10 @@ export interface ConfigModuleOptions {
load?: Array<ConfigFactory>;

/**
* 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;
}
28 changes: 28 additions & 0 deletions 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();
});
});
12 changes: 12 additions & 0 deletions tests/src/app.module.ts
Expand Up @@ -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,
Expand Down

0 comments on commit b725805

Please sign in to comment.