Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expand): add dotenv-expand options support #891

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/config.module.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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