diff --git a/SetEnv.ts b/SetEnv.ts deleted file mode 100644 index 8bd714f..0000000 --- a/SetEnv.ts +++ /dev/null @@ -1,25 +0,0 @@ -import * as path from 'path' -import * as dotenv from 'dotenv' - -import logger from './logger' - -class SetEnv { - constructor() { - const environment = process.env.NODE_ENV - const configurations = { path: path.resolve(process.cwd(), '.env') } - - if (environment) { - configurations.path = 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!') - } - } -} - -export default new SetEnv() diff --git a/index.ts b/index.ts index c4dfda5..2e59717 100644 --- a/index.ts +++ b/index.ts @@ -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' diff --git a/package-lock.json b/package-lock.json index 20f8461..d61eb7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,18 @@ { "name": "@secjs/env", - "version": "1.1.9", + "version": "1.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@secjs/env", - "version": "1.1.9", + "version": "1.2.0", "license": "MIT", "dependencies": { "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", @@ -1105,9 +1105,9 @@ } }, "node_modules/@secjs/logger": { - "version": "1.1.9", - "resolved": "https://registry.npmjs.org/@secjs/logger/-/logger-1.1.9.tgz", - "integrity": "sha512-B4MHV5VZ3bXOm1AoZQNTWCo3kaJuVxON9p65U8bD8dhrgakOlDgbJLE6AwFMGaCvH8Dq2MWaxojwO9ObM5w+CQ==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@secjs/logger/-/logger-1.2.1.tgz", + "integrity": "sha512-Tcf4bD8EHZXfFHDKuKWmJX9GXpSbN/1AgrTZcMpQun3DGLX2+s188xQ7nQZen8oeSUud8g8+/wicb1tQGWmOxw==", "dev": true, "dependencies": { "chalk": "4.1.1", @@ -8465,9 +8465,9 @@ } }, "@secjs/logger": { - "version": "1.1.9", - "resolved": "https://registry.npmjs.org/@secjs/logger/-/logger-1.1.9.tgz", - "integrity": "sha512-B4MHV5VZ3bXOm1AoZQNTWCo3kaJuVxON9p65U8bD8dhrgakOlDgbJLE6AwFMGaCvH8Dq2MWaxojwO9ObM5w+CQ==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@secjs/logger/-/logger-1.2.1.tgz", + "integrity": "sha512-Tcf4bD8EHZXfFHDKuKWmJX9GXpSbN/1AgrTZcMpQun3DGLX2+s188xQ7nQZen8oeSUud8g8+/wicb1tQGWmOxw==", "dev": true, "requires": { "chalk": "4.1.1", diff --git a/package.json b/package.json index a63da92..ce73bbc 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -146,6 +146,8 @@ } }, "files": [ + "src/*.js", + "src/*.d.ts", "*.js", "*.d.ts" ] diff --git a/src/DotEnvResolver.ts b/src/DotEnvResolver.ts new file mode 100644 index 0000000..0c5344e --- /dev/null +++ b/src/DotEnvResolver.ts @@ -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!') +} diff --git a/src/Env.ts b/src/Env.ts new file mode 100644 index 0000000..a10efa0 --- /dev/null +++ b/src/Env.ts @@ -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 +} diff --git a/global.ts b/src/utils/global.ts similarity index 84% rename from global.ts rename to src/utils/global.ts index 29ffb1b..b63b5e3 100644 --- a/global.ts +++ b/src/utils/global.ts @@ -1,4 +1,4 @@ -import Env, { IEnv } from './index' +import Env, { IEnv } from '../Env' export {} diff --git a/logger.ts b/src/utils/logger.ts similarity index 79% rename from logger.ts rename to src/utils/logger.ts index c6e1716..9ca7551 100644 --- a/logger.ts +++ b/src/utils/logger.ts @@ -9,6 +9,6 @@ export default new Logger( 'Debug', new LogMapper( [new DebugFormatter()], - [new DebugTransporter('api:environment')], + [new DebugTransporter('api:environments')], ), ) diff --git a/tests/env.spec.ts b/tests/env.spec.ts index 2277e31..f1ca3f8 100644 --- a/tests/env.spec.ts +++ b/tests/env.spec.ts @@ -1,4 +1,4 @@ -import Env from '../index' +import Env from '../src/Env' describe('\n Env 🔁', () => { let DB_USERNAME = ''