Skip to content

Commit

Permalink
feat: Add global
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Nov 3, 2021
1 parent 2718a83 commit 92594a0
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 79 deletions.
25 changes: 0 additions & 25 deletions SetEnv.ts

This file was deleted.

41 changes: 1 addition & 40 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1 @@
import './SetEnv'
import logger from './logger'

export interface IEnv {
name: string
type: string
}

/**
* Get the value of an environment variable
*
* @param env The env name or object with env name and type
* @param defaultValue The default value of the env if env does not exist
* @throws Error if env type is not IEnv or string
* @return The value of the environment or defaultValue
*/
export default function Env(
env: string | IEnv,
defaultValue: string | number | boolean,
): string | number | boolean | any {
const environment = process.env[`${typeof env === 'string' ? env : env.name}`]

if (!environment) {
logger.debug(`Variable ${env} not found`)

return defaultValue
}

if (typeof env === 'object') {
if (env.type === 'number') return parseInt(environment)
if (env.type === 'boolean') return environment == 'true'
if (env.type === 'object') return JSON.parse(environment)

logger.debug(`Type ${env.type} not found, returning default value`)

return defaultValue
}

return environment
}
export * from './src/Env'
18 changes: 9 additions & 9 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/env",
"version": "1.1.9",
"version": "1.2.0",
"license": "MIT",
"author": "João Lenon",
"repository": "https://github.com/SecJS/Env.git",
Expand All @@ -23,7 +23,7 @@
"dotenv": "10.0.0"
},
"devDependencies": {
"@secjs/logger": "1.1.9",
"@secjs/logger": "1.2.1",
"@types/debug": "4.1.7",
"@types/jest": "27.0.1",
"@types/supertest": "2.0.11",
Expand Down Expand Up @@ -146,6 +146,8 @@
}
},
"files": [
"src/*.js",
"src/*.d.ts",
"*.js",
"*.d.ts"
]
Expand Down
20 changes: 20 additions & 0 deletions src/DotEnvResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as dotenv from 'dotenv'

import logger from './utils/logger'

import { resolve } from 'path'

const environment = process.env.NODE_ENV
const configurations = { path: resolve(process.cwd(), '.env') }

if (environment) {
configurations.path = resolve(process.cwd(), `.env.${environment}`)

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

const result = dotenv.config(configurations)

if (result.error) {
logger.debug('Any environment variable file found!')
}
42 changes: 42 additions & 0 deletions src/Env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import './DotEnvResolver'

import logger from './utils/logger'

export interface IEnv {
name: string
type: string
}

/**
* Get the value of an environment variable
*
* @param env The env name or object with env name and type
* @param defaultValue The default value of the env if env does not exist
* @throws Error if env type is not IEnv or string
* @return The value of the environment or defaultValue
*/
export default function Env(
env: string | IEnv,
defaultValue: string | number | boolean,
): string | number | boolean | any {
const environment = process.env[`${typeof env === 'string' ? env : env.name}`]

if (!environment) {
logger.debug(`Variable ${env} not found`)

return defaultValue
}

if (typeof env === 'object') {
if (env.type === 'number') return parseInt(environment)
// eslint-disable-next-line eqeqeq
if (env.type === 'boolean') return environment == 'true'
if (env.type === 'object') return JSON.parse(environment)

logger.debug(`Type ${env.type} not found, returning default value`)

return defaultValue
}

return environment
}
2 changes: 1 addition & 1 deletion global.ts → src/utils/global.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Env, { IEnv } from './index'
import Env, { IEnv } from '../Env'

export {}

Expand Down
2 changes: 1 addition & 1 deletion logger.ts → src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default new Logger(
'Debug',
new LogMapper(
[new DebugFormatter()],
[new DebugTransporter('api:environment')],
[new DebugTransporter('api:environments')],
),
)
2 changes: 1 addition & 1 deletion tests/env.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Env from '../index'
import Env from '../src/Env'

describe('\n Env 🔁', () => {
let DB_USERNAME = ''
Expand Down

0 comments on commit 92594a0

Please sign in to comment.