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
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/config",
"version": "1.1.8",
"version": "1.1.9",
"description": "Cache and handle environment variables and config files of Athenna.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
52 changes: 30 additions & 22 deletions src/Helpers/EnvHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export class EnvHelper {
* @return {string}
*/
static setEnvInEnv(environment, autoCast) {
if (!environment) {
return undefined
}

if (!Is.String(environment)) {
return environment
}
Expand Down Expand Up @@ -56,9 +52,12 @@ export class EnvHelper {
}

/**
* Cast the environment variable if matches
* the cast reserved vars -> (). If the var
* doesnt match, returns the same variable value.
* Cast the environment variable if it values matches a
* number string, boolean string or json string. Also,
* if the variable has the reserved vars -> () enclosed
* it will be removed and returned as string. So the value
* (false) in .env is equals to 'false' but the value 10 is
* equals to number 10.
*
* @param {string} environment
* @return {any}
Expand All @@ -67,15 +66,19 @@ export class EnvHelper {
if (environment.match(/\((.*?)\)/)) {
environment = environment.slice(1, -1)

/**
* Is.Json(environment) will be true if values
* are boolean string and also number string.
*
* JSON.parse also can cast this type of values.
*/
if (Is.Json(environment)) {
return JSON.parse(environment)
}
return environment
}

if (/^-?\d+$/.test(environment)) {
return +environment
}

if (Is.Json(environment)) {
return JSON.parse(environment)
}

if (environment === 'true' || environment === 'false') {
return environment === 'true'
}

return environment
Expand Down Expand Up @@ -108,11 +111,16 @@ export class EnvHelper {
* @return {""|boolean}
*/
static isToOverrideEnvs() {
return (
process.env.OVERRIDE_ENV &&
(process.env.OVERRIDE_ENV === true ||
process.env.OVERRIDE_ENV === 'true' ||
process.env.OVERRIDE_ENV === '(true)')
)
return this.isEnvTrue(process.env.OVERRIDE_ENV)
}

/**
* Verify if the env variable is true, 'true' or '(true)'.
*
* @param {string} env
* @return {boolean}
*/
static isEnvTrue(env) {
return env && (env === true || env === 'true' || env === '(true)')
}
}
6 changes: 3 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ export class Config {
* Get the value from config file by key. If not
* found, defaultValue will be used.
*
* @param {string} key
* @param {any,undefined} defaultValue
* @param {string} [key]
* @param {any,undefined} [defaultValue]
* @return {any}
*/
static get(key: string, defaultValue?: any): any
static get(key?: string, defaultValue?: any): any

/**
* Load all configuration files in path.
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ export class Config {
* Get the value from config file by key. If not
* found, defaultValue will be used.
*
* @param {string} key
* @param {any,undefined} defaultValue
* @param {string} [key]
* @param {any,undefined} [defaultValue]
* @return {any}
*/
static get(key, defaultValue = undefined) {
if (!key) {
const config = {}

for (const [key, value] of this.configs.entries()) {
config[key] = value
}

return config
}

const [mainKey, ...keys] = key.split('.')

const config = this.configs.get(mainKey)
Expand Down
12 changes: 8 additions & 4 deletions tests/Stubs/.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
ENV="production"
NUMBER_ENV="(10)"
BOOLEAN_ENV="(true)"
OBJECT_ENV="({"name":"Paulo"})"
NUMBER_ENV="10"
STRING_NUMBER_ENV="(10)"
BOOLEAN_ENV="true"
STRING_BOOLEAN_ENV="(true)"
OBJECT_ENV="{"name":"Paulo"}"
STRING_OBJECT_ENV="({"name":"Paulo"})"
ENV_IN_ENV="${NUMBER_ENV}-${BOOLEAN_ENV}"
ENV_IN_ENV_JSON="({ "maintainers": ${OBJECT_ENV} })"
ENV_IN_ENV_JSON="{ "maintainers": ${OBJECT_ENV} }"
STRING_ENV_IN_ENV_JSON="({ "maintainers": ${OBJECT_ENV} })"
TEST_ENV="(true)"
PRESET="false"
9 changes: 8 additions & 1 deletion tests/Unit/ConfigTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ test.group('ConfigTest', group => {
await new Folder(Path.config()).remove()
})

test('should be able to get full configurations values from Config class', ({ assert }) => {
test('should be able to get all configurations values from Config class', ({ assert }) => {
const configs = Config.get()

assert.deepEqual(configs.app, { name: 'Athenna', env: 'test' })
assert.deepEqual(configs.database, { username: 'Athenna' })
})

test('should be able to get full configurations values of one file from Config class', ({ assert }) => {
const app = Config.get('app')

assert.deepEqual(app, {
Expand Down
13 changes: 9 additions & 4 deletions tests/Unit/EnvTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test.group('EnvTest', group => {
process.env.NODE_ENV = 'testing'
EnvHelper.resolveFile()

assert.equal(Env('PRESET'), 'true')
assert.equal(Env('PRESET'), true)
})

test('should override pre set environment variables', async ({ assert }) => {
Expand All @@ -43,7 +43,7 @@ test.group('EnvTest', group => {
process.env.NODE_ENV = 'testing'
EnvHelper.resolveFile()

assert.equal(Env('PRESET'), 'false')
assert.equal(Env('PRESET'), false)
})

test('should be able to resolve more than one env files at same time', async ({ assert }) => {
Expand All @@ -62,12 +62,15 @@ test.group('EnvTest', group => {
EnvHelper.resolveFile()

assert.strictEqual(Env('NUMBER_ENV'), 10)
assert.strictEqual(Env('STRING_NUMBER_ENV'), '10')
assert.strictEqual(Env('BOOLEAN_ENV'), true)
assert.strictEqual(Env('STRING_BOOLEAN_ENV'), 'true')
assert.deepEqual(Env('OBJECT_ENV'), { name: 'Paulo' })
assert.deepEqual(Env('STRING_OBJECT_ENV'), `{"name":"Paulo"}`)
})

test('should be able to fallback to default values when the env does not exist', async ({ assert }) => {
assert.equal(Env('NO_EXIST', 'Hello World'), 'Hello World')
assert.deepEqual(Env('NO_EXIST', false), false)
})

test('should be able to set env values inside of other env values', async ({ assert }) => {
Expand All @@ -76,13 +79,15 @@ test.group('EnvTest', group => {

assert.equal(Env('ENV_IN_ENV'), '10-true')
assert.deepEqual(Env('ENV_IN_ENV_JSON'), { maintainers: { name: 'Paulo' } })
assert.deepEqual(Env('STRING_ENV_IN_ENV_JSON'), `{ "maintainers": {"name":"Paulo"} }`)
})

test('should be able to turn off the auto cast for specifics envs when needed', async ({ assert }) => {
process.env.NODE_ENV = ''
EnvHelper.resolveFile()

assert.equal(Env('NUMBER_ENV', '10', false), '(10)')
assert.equal(Env('NUMBER_ENV', '10', false), '10')
assert.equal(Env('STRING_NUMBER_ENV', '(10)', false), '(10)')
})

test('should be able to use Env without any .env file', async ({ assert }) => {
Expand Down