Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DB_SYNC="false"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ out

# dotenv environment variables file
.env
.env.testing
#.env.testing
.env.production

# Build files
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './src/Env'
export * from './src/utils/resolveEnvFile'
82 changes: 65 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/env",
"version": "1.2.5",
"version": "1.2.6",
"license": "MIT",
"author": "João Lenon",
"repository": "https://github.com/SecJS/Env.git",
Expand All @@ -23,7 +23,9 @@
"dotenv": "10.0.0"
},
"devDependencies": {
"@secjs/exceptions": "^1.0.4",
"@secjs/logger": "1.2.1",
"@secjs/utils": "^1.5.2",
"@types/debug": "4.1.7",
"@types/jest": "27.0.1",
"@types/supertest": "2.0.11",
Expand All @@ -49,7 +51,8 @@
"ts-loader": "9.2.3",
"ts-node": "10.0.0",
"tsconfig-paths": "3.9.0",
"typescript": "4.3.5"
"typescript": "4.3.5",
"uuid": "^8.3.2"
},
"lint-staged": {
"*.js": [
Expand Down
20 changes: 0 additions & 20 deletions src/DotEnvResolver.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/Env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import './DotEnvResolver'

import logger from './utils/logger'

export interface IEnv {
Expand Down
3 changes: 3 additions & 0 deletions src/utils/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Env, IEnv } from '../Env'
import { resolveEnvFile } from './resolveEnvFile'

resolveEnvFile()

export {}

Expand Down
27 changes: 27 additions & 0 deletions src/utils/resolveEnvFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as dotenv from 'dotenv'

import logger from './logger'
import { Path } from '@secjs/utils'

export function resolveEnvFile() {
const environment = process.env.NODE_ENV
const configurations = { path: Path.noBuild().pwd('.env') }

if (environment) {
configurations.path = Path.noBuild().pwd(`.env.${environment}`)

logger.debug(`Environment variables set using .env.${environment} file`)
}

const result = dotenv.config(configurations)

if (result.error) {
logger.debug('Any environment variable file found!')

return
}

if (result.parsed) {
Object.keys(result.parsed).forEach(k => (process.env[k] = result.parsed[k]))
}
}
12 changes: 12 additions & 0 deletions tests/env.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Env } from '../src/Env'
import { resolveEnvFile } from '../src/utils/resolveEnvFile'

describe('\n Env 🔁', () => {
let DB_USERNAME = ''
Expand All @@ -11,6 +12,7 @@ describe('\n Env 🔁', () => {
process.env.DB_USERNAME = 'user'
process.env.DB_PASSWORD = 'pass'
process.env.OBJECT = '{"joao":"joao"}'
process.env.DB_SYNC = 'true'

DB_USERNAME = process.env.DB_USERNAME
DB_PASSWORD = process.env.DB_PASSWORD
Expand Down Expand Up @@ -48,4 +50,14 @@ describe('\n Env 🔁', () => {
expect(DB_DEBUG).toBe(false)
expect(OBJECT.joao).toBe('joao')
})

it('should resolve the .env.testing file and subscribe all envs from process.env that are in this file', () => {
process.env.NODE_ENV = 'testing'

resolveEnvFile()

const DB_SYNC = Env('DB_SYNC', '')

expect(DB_SYNC).toBe('false')
})
})