Skip to content

Commit

Permalink
feat(): support loading multiple env files
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim committed Mar 9, 2020
1 parent b55a3a8 commit b9eac42
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
28 changes: 14 additions & 14 deletions lib/config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,22 @@ export class ConfigModule {
private static loadEnvFile(
options: ConfigModuleOptions,
): Record<string, any> {
try {
const envFilePath = options.envFilePath || resolve(process.cwd(), '.env');
const config = dotenv.parse(fs.readFileSync(envFilePath));
if (options.expandVariables) {
const configExpandedVars = {
parsed: config,
};
return dotenvExpand(configExpandedVars).parsed || {};
}
return config;
} catch (err) {
if (options.envFilePath || (err && err.code !== 'ENOENT')) {
throw err;
const envFilePaths = Array.isArray(options.envFilePath)
? options.envFilePath
: [options.envFilePath || resolve(process.cwd(), '.env')];
let config: ReturnType<typeof dotenv.parse> = {};
for (const envFilePath of envFilePaths) {
if (fs.existsSync(envFilePath)) {
config = Object.assign(
dotenv.parse(fs.readFileSync(envFilePath)),
config,
);
if (options.expandVariables) {
config = dotenvExpand({ parsed: config }).parsed || config;
}
}
return {};
}
return config;
}

private static assignVariablesToProcess(config: Record<string, any>) {
Expand Down
4 changes: 2 additions & 2 deletions lib/interfaces/config-module-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export interface ConfigModuleOptions {
ignoreEnvVars?: boolean;

/**
* Path to the environment file to be loaded.
* Path to the environment file(s) to be loaded.
*/
envFilePath?: string;
envFilePath?: string | string[];

/**
* Environment file encoding.
Expand Down
26 changes: 26 additions & 0 deletions tests/e2e/load-multiple-env.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { AppModule } from '../src/app.module';

describe('Environment variables (multiple env files)', () => {
let app: INestApplication;

beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [AppModule.withMultipleEnvFiles()],
}).compile();

app = module.createNestApplication();
await app.init();
});

it(`should return loaded env variables`, () => {
const envVars = app.get(AppModule).getEnvVariables();
expect(envVars.PORT).toEqual('3000');
expect(envVars.TIMEOUT).toEqual('5000');
});

afterEach(async () => {
await app.close();
});
});
3 changes: 2 additions & 1 deletion tests/src/.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PORT=4000
PORT=4000
TIMEOUT=5000
1 change: 1 addition & 0 deletions tests/src/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=3000
11 changes: 11 additions & 0 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ export class AppModule {
};
}

static withMultipleEnvFiles(): DynamicModule {
return {
module: AppModule,
imports: [
ConfigModule.forRoot({
envFilePath: [join(__dirname, '.env.local'), join(__dirname, '.env')],
}),
],
};
}

static withLoadedConfigurations(): DynamicModule {
return {
module: AppModule,
Expand Down

0 comments on commit b9eac42

Please sign in to comment.