From 4d84bbaa6dbbfc330b7c2be4844b634944b339f7 Mon Sep 17 00:00:00 2001 From: Maximiliano Delgado Date: Sun, 28 Jul 2024 18:43:20 -0300 Subject: [PATCH] chore(envs): add function tu return env file path by NODE_ENV var --- src/app.module.ts | 4 ++-- src/config/environments.ts | 5 ++++- src/main.ts | 16 ++++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index 425fd97..8571c3c 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -17,12 +17,12 @@ import { CharactersModule } from './characters/characters.module'; import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { config, environments, validationSchema } from './config'; +import { getEnvFilePath, config, validationSchema } from './config'; @Module({ imports: [ ConfigModule.forRoot({ - envFilePath: environments[`${process.env.NODE_ENV}`], + envFilePath: getEnvFilePath(), ignoreEnvFile: process.env.NODE_ENV === 'production' || false, load: [config], isGlobal: true, diff --git a/src/config/environments.ts b/src/config/environments.ts index 3d5ed4e..f4cbcfd 100644 --- a/src/config/environments.ts +++ b/src/config/environments.ts @@ -1,7 +1,10 @@ -export const environments = { +// List of environments +const environments = { test: '.env.test', dev: '.env.dev', qa: '.env.qa', stg: '.env.stg', production: '.env', }; + +export const getEnvFilePath = (): string => environments[`${process.env.NODE_ENV}`]; diff --git a/src/main.ts b/src/main.ts index b967906..d89d600 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,24 +1,24 @@ import { NestFactory } from '@nestjs/core'; import { Logger, ValidationPipe } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; -import { ExceptionsFilter } from '@tresdoce-nestjs-toolkit/paas'; import cookieParser from 'cookie-parser'; import compression from 'compression'; import helmet from 'helmet'; +import { DocumentBuilder, OpenAPIObject, SwaggerModule } from '@nestjs/swagger'; +import { ExceptionsFilter } from '@tresdoce-nestjs-toolkit/paas'; +import { otelProvider } from '@tresdoce-nestjs-toolkit/tracing'; import { AppModule } from './app.module'; -import { otelProvider } from '@tresdoce-nestjs-toolkit/tracing'; import { config } from './config'; -async function bootstrap() { +async function bootstrap(): Promise { otelProvider(config().tracing); const app = await NestFactory.create(AppModule, { logger: new Logger(), }); const appConfig = app.get(ConfigService)['internalConfig']['config']; const { server, swagger, project } = appConfig; - const port = parseInt(server.port, 10) || 8080; + const port: number = parseInt(server.port, 10) || 8080; app.setGlobalPrefix(`${server.context}`); @@ -47,7 +47,7 @@ async function bootstrap() { .setContact(project.author.name, project.author.url, project.author.email) .addServer(`/${server.context}`) .build(); - const document = SwaggerModule.createDocument(app, config, { + const document: OpenAPIObject = SwaggerModule.createDocument(app, config, { ignoreGlobalPrefix: true, }); SwaggerModule.setup(`${server.context}/${swagger.path}`, app, document, {}); @@ -62,7 +62,7 @@ async function bootstrap() { }); } - await app.listen(port, async () => { + await app.listen(port, async (): Promise => { const appServer = `http://localhost:${port}/${server.context}`; if (swagger.enabled) { Logger.log(`📚 Swagger is running on: ${appServer}/${swagger.path}`, `${project.name}`); @@ -71,4 +71,4 @@ async function bootstrap() { }); } -(async () => await bootstrap())(); +(async (): Promise => await bootstrap())();