-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
79 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = '' | ||
|