Skip to content

Commit

Permalink
Aplicando configurações de environment
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-dourado-atlantico committed Jul 23, 2024
1 parent 1c9afc1 commit 97c2c29
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
2 changes: 2 additions & 0 deletions environment/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=
NODE_ENV=
2 changes: 2 additions & 0 deletions environment/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=3000
NODE_ENV=development
2 changes: 2 additions & 0 deletions environment/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PORT=3000
NODE_ENV=test
8 changes: 7 additions & 1 deletion nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
"deleteOutDir": true,
"assets": [
{
"include": "../environment/.env*",
"outDir": "./dist/environment"
}
]
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:dev": "NODE_ENV=development nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
Expand Down
6 changes: 3 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { AppService } from './app.service'
import { EnvConfigModule } from './shared/infrastructure/env-config/env-config.module'

@Module({
imports: [EnvConfigModule],
controllers: [AppController],
providers: [AppService],
imports: [EnvConfigModule.forRoot()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Test, TestingModule } from '@nestjs/testing'
import { EnvConfigService } from '../../env-config.service'
import { EnvConfigModule } from '../../env-config.module'

describe('EnvConfigService', () => {
let sut: EnvConfigService

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [EnvConfigModule.forRoot()],
providers: [EnvConfigService],
}).compile()

sut = module.get<EnvConfigService>(EnvConfigService)
})

it('should be defined', () => {
expect(sut).toBeDefined()
})

it('deve retornar a variavel PORT', () => {
expect(sut.getAppPort()).toBe(3000)
})

it('deve retornar a variavel NODE_ENV', () => {
expect(sut.getNodeEnv()).toBe('test')
})
})
21 changes: 17 additions & 4 deletions src/shared/infrastructure/env-config/env-config.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { Module } from '@nestjs/common';
import { EnvConfigService } from './env-config.service';
import { DynamicModule, Module } from '@nestjs/common'
import { EnvConfigService } from './env-config.service'
import { ConfigModule, ConfigModuleOptions } from '@nestjs/config'
import { join } from 'node:path'

@Module({
providers: [EnvConfigService]
providers: [EnvConfigService],
exports: [EnvConfigService],
})
export class EnvConfigModule {}
export class EnvConfigModule extends ConfigModule {
static forRoot(options: ConfigModuleOptions = {}): DynamicModule {
return super.forRoot({
...options,
envFilePath: [
// join(__dirname, `../../../../environment/.env`),
join(__dirname, `../../../../environment/.env.${process.env.NODE_ENV}`),
],
})
}
}
18 changes: 0 additions & 18 deletions src/shared/infrastructure/env-config/env-config.service.spec.ts

This file was deleted.

0 comments on commit 97c2c29

Please sign in to comment.