Skip to content

Commit

Permalink
fix(): load env file variables over system env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed May 7, 2020
1 parent f2e8180 commit 6efcc5a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
5 changes: 3 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"trailingComma": "all",
"singleQuote": true
}
"singleQuote": true,
"arrowParens": "avoid"
}
16 changes: 10 additions & 6 deletions lib/config.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export class ConfigModule {
let config = this.loadEnvFile(options);
if (!options.ignoreEnvVars) {
config = {
...config,
...process.env,
...config,
};
}
const validationOptions = this.getSchemaValidationOptions(options);
Expand All @@ -65,12 +65,12 @@ export class ConfigModule {
}
const isConfigToLoad = options.load && options.load.length;
const providers = (options.load || [])
.map((factory) =>
.map(factory =>
createConfigProvider(factory as ConfigFactory & ConfigFactoryKeyHost),
)
.filter((item) => item) as FactoryProvider[];
.filter(item => item) as FactoryProvider[];

const configProviderTokens = providers.map((item) => item.provide);
const configProviderTokens = providers.map(item => item.provide);
const configServiceProvider = {
provide: ConfigService,
useFactory: (configService: ConfigService) => configService,
Expand Down Expand Up @@ -173,8 +173,12 @@ export class ConfigModule {
if (!isObject(config)) {
return;
}
const keys = Object.keys(config).filter((key) => !(key in process.env));
keys.forEach((key) => (process.env[key] = config[key]));
const keys = Object.keys(config).filter(
key =>
!(key in process.env) ||
(key in process.env && process.env[key] !== config[key]),
);
keys.forEach(key => (process.env[key] = config[key]));
}

private static mergePartial(
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/load-priority.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ describe('Environment variables and .env files', () => {
await app.init();
});

it('should choose env var over dotenv', () => {
it('should choose dotenv over env vars', () => {
const configService = app.get(ConfigService);
expect(configService.get('PORT')).toEqual('8000');
expect(configService.get('PORT')).toEqual('4000');
});
});

Expand All @@ -58,9 +58,9 @@ describe('Environment variables and .env files', () => {
await app.init();
});

it('should choose env var over dotenv', () => {
it('should choose dotenv over env vars', () => {
const configService = app.get(ConfigService);
expect(configService.get('PORT')).toEqual(8000);
expect(configService.get('PORT')).toEqual(4000);
});
});

Expand Down

0 comments on commit 6efcc5a

Please sign in to comment.