@athenna/config
Advanced tools
Comparing version 3.1.2 to 3.2.0
@@ -48,2 +48,6 @@ /** | ||
/** | ||
* Set a value in the configuration key if value is not defined. | ||
*/ | ||
static safeSet(key: string, value: any | any[]): typeof Config; | ||
/** | ||
* Delete the configuration key. | ||
@@ -50,0 +54,0 @@ */ |
@@ -9,6 +9,4 @@ /** | ||
*/ | ||
import check from 'syntax-error'; | ||
import { File, Json, Path, Folder, Module, } from '@athenna/common'; | ||
import { parse } from 'node:path'; | ||
import { ConfigSyntaxException } from '#src/Exceptions/ConfigSyntaxException'; | ||
import { File, Json, Path, Folder, Exec } from '@athenna/common'; | ||
import { RecursiveConfigException } from '#src/Exceptions/RecursiveConfigException'; | ||
@@ -79,2 +77,12 @@ export class Config { | ||
/** | ||
* Set a value in the configuration key if value is not defined. | ||
*/ | ||
static safeSet(key, value) { | ||
if (this.configs.exists(key)) { | ||
return this; | ||
} | ||
this.configs.set(key, value); | ||
return this; | ||
} | ||
/** | ||
* Delete the configuration key. | ||
@@ -101,4 +109,3 @@ */ | ||
const { files } = await new Folder(path).load(); | ||
const promises = files.map(file => safe ? this.safeLoad(file.path) : this.load(file.path)); | ||
await Promise.all(promises); | ||
await Exec.concurrently(files, file => safe ? this.safeLoad(file.path) : this.load(file.path)); | ||
} | ||
@@ -135,8 +142,2 @@ /** | ||
const fileContent = file.getContentSync().toString(); | ||
const syntaxErr = check(fileContent, file.href, { | ||
sourceType: 'module', | ||
}); | ||
if (syntaxErr) { | ||
throw new ConfigSyntaxException(syntaxErr, file.base); | ||
} | ||
if (fileContent.includes('Config.get')) { | ||
@@ -156,5 +157,5 @@ const matches = fileContent.match(/Config.get\(([^)]+)\)/g); | ||
*/ | ||
const versionedPath = `${file.href}?version=${Math.random()}`; | ||
this.configs.set(name, await Module.get(import(versionedPath))); | ||
file.href = `${file.href}?version=${Math.random()}`; | ||
this.configs.set(name, await file.import()); | ||
} | ||
} |
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
static resolveFile(): void; | ||
static resolveFile(lookupNodeEnv?: boolean): void; | ||
/** | ||
@@ -45,2 +45,11 @@ * Resolve some env file path. | ||
static isEnvTrue(env: string): boolean; | ||
/** | ||
* Get the NODE_ENV variable from process.env or from the | ||
* .env file if exists in project root. | ||
*/ | ||
static getNodeEnv(lookupNodeEnv: boolean): string; | ||
/** | ||
* Verify if some environment is defined ignoring. | ||
*/ | ||
static isDefinedEnv(environment: string): boolean; | ||
} |
@@ -11,3 +11,3 @@ /** | ||
import { Env } from '#src'; | ||
import { Is, Path } from '@athenna/common'; | ||
import { File, Is, Path } from '@athenna/common'; | ||
export class EnvHelper { | ||
@@ -55,8 +55,8 @@ /** | ||
} | ||
if (environment === 'true' || environment === 'false') { | ||
return environment === 'true'; | ||
} | ||
if (Is.Json(environment)) { | ||
return JSON.parse(environment); | ||
} | ||
if (environment === 'true' || environment === 'false') { | ||
return environment === 'true'; | ||
} | ||
return environment; | ||
@@ -68,4 +68,4 @@ } | ||
*/ | ||
static resolveFile() { | ||
const environment = process.env.NODE_ENV; | ||
static resolveFile(lookupNodeEnv = false) { | ||
const environment = this.getNodeEnv(lookupNodeEnv); | ||
const configurations = { | ||
@@ -75,3 +75,3 @@ path: Path.pwd('.env'), | ||
}; | ||
if (environment && environment !== '' && environment !== 'production') { | ||
if (environment) { | ||
configurations.path = Path.pwd(`.env.${environment}`); | ||
@@ -100,2 +100,44 @@ } | ||
} | ||
/** | ||
* Get the NODE_ENV variable from process.env or from the | ||
* .env file if exists in project root. | ||
*/ | ||
static getNodeEnv(lookupNodeEnv) { | ||
if (this.isDefinedEnv(process.env.NODE_ENV)) { | ||
return process.env.NODE_ENV; | ||
} | ||
if (!lookupNodeEnv) { | ||
return null; | ||
} | ||
const file = new File(Path.pwd('.env'), ''); | ||
if (!file.fileExists) { | ||
return null; | ||
} | ||
const content = file.getContentAsStringSync(); | ||
if (content && content.includes('NODE_ENV=')) { | ||
let value = content.split('NODE_ENV=')[1]; | ||
if (value.includes('\n')) { | ||
value = value.split('\n')[0]; | ||
} | ||
const serializedValue = value | ||
.replace(/'/g, '') | ||
.replace(/"/g, '') | ||
.replace(/\r/g, ''); | ||
if (this.isDefinedEnv(serializedValue)) { | ||
process.env.NODE_ENV = serializedValue; | ||
return process.env.NODE_ENV; | ||
} | ||
return null; | ||
} | ||
return null; | ||
} | ||
/** | ||
* Verify if some environment is defined ignoring. | ||
*/ | ||
static isDefinedEnv(environment) { | ||
return (environment && | ||
environment !== '' && | ||
environment !== 'undefined' && | ||
environment !== 'production'); | ||
} | ||
} |
{ | ||
"name": "@athenna/config", | ||
"version": "3.1.2", | ||
"version": "3.2.0", | ||
"description": "Cache and handle environment variables and config files of Athenna.", | ||
@@ -19,6 +19,7 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "ts-node bin/build.ts", | ||
"node": "cross-env NODE_OPTIONS=\"--experimental-import-meta-resolve\" ts-node", | ||
"build": "npm run node --silent -- bin/build.ts", | ||
"lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix", | ||
"test": "npm run --silent lint:fix && ts-node bin/test.ts", | ||
"test:debug": "cross-env DEBUG=api:* ts-node bin/test.ts --inspect", | ||
"test": "npm run --silent lint:fix && npm run node --silent -- bin/test.ts", | ||
"test:debug": "cross-env DEBUG=api:* npm run node --silent -- bin/test.ts --inspect", | ||
"test:coverage": "c8 npm run --silent test" | ||
@@ -46,7 +47,8 @@ }, | ||
"dependencies": { | ||
"dotenv": "^16.0.0", | ||
"dotenv": "^16.0.3", | ||
"syntax-error": "^1.4.0" | ||
}, | ||
"devDependencies": { | ||
"@athenna/common": "^3.0.0", | ||
"@athenna/common": "^3.3.0", | ||
"@athenna/test": "^3.1.1", | ||
"@japa/assert": "^1.3.6", | ||
@@ -76,2 +78,3 @@ "@japa/run-failed-tests": "^1.1.0", | ||
"prettier": "^2.8.3", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^3.0.2", | ||
@@ -78,0 +81,0 @@ "ts-node": "^10.9.1", |
25066
577
30
17
7
Updateddotenv@^16.0.3