Skip to content

Commit

Permalink
Refactor: refactor get NODE_ENV
Browse files Browse the repository at this point in the history
NODE_ENV를 한번만 로드하고 사용할 수 있도록 수정
- 중복 코드 제거

Refactor: refactor get NODE_ENV

Refactor: refactor get NODE_ENV
  • Loading branch information
sally0226 committed Jun 29, 2024
1 parent 85c5250 commit 2275346
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MikroOrmModule } from '@mikro-orm/nestjs';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { getEnvPath, ignoreEnvFile } from './common/helper/env.helper';
import { getNodeEnv, isIgnoreEnvFile } from './common/helper/env.helper';
import { envValidation } from './common/helper/env.validation';
import { MapModule } from './map/map.module';
import { UserMapModule } from './user-map/user-map.module';
Expand All @@ -16,10 +16,10 @@ import { UserModule } from './user/user.module';
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: getEnvPath(`${__dirname}/..`),
envFilePath: `${__dirname}/../.${getNodeEnv}.env`,
cache: true,
validate: envValidation,
ignoreEnvFile: ignoreEnvFile,
ignoreEnvFile: isIgnoreEnvFile,
}),
MikroOrmModule.forRoot(),
AuthModule,
Expand Down
25 changes: 11 additions & 14 deletions src/common/helper/env.helper.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { resolve } from 'path';
import { InternalServerErrorException } from '@nestjs/common';

import { NODE_ENVIRONMENT } from './env.validation';

export const getEnvPath = (dest: string): string => {
const env: string = process.env.NODE_ENV;

if (env == nodeEnvironment.staging || env == nodeEnvironment.production) {
return undefined;
}

const filename: string = env ? `.${env}.env` : '.development.env';
return resolve(`${dest}/${filename}`);
};

export const ignoreEnvFile = ((): boolean => {
export const getNodeEnv = (() => {
const env = process.env.NODE_ENV;
return env === NODE_ENVIRONMENT.stage || env === NODE_ENVIRONMENT.production;
const nodeEnv = NODE_ENVIRONMENT[env];
if (nodeEnv === undefined) {
throw new InternalServerErrorException('Unknown NODE_ENV');
}
return nodeEnv;
})();

export const isIgnoreEnvFile =
getNodeEnv === NODE_ENVIRONMENT.stage ||
getNodeEnv === NODE_ENVIRONMENT.production;
2 changes: 1 addition & 1 deletion src/common/helper/env.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const NODE_ENVIRONMENT = {
[stage]: stage,
[production]: production,
[test]: test,
};
} as const;

export class EnvironmentVariables {
@IsEnum(NODE_ENVIRONMENT)
Expand Down

0 comments on commit 2275346

Please sign in to comment.