From 8fa04a7ac373e1949b040a7d20d59aee8dbf9304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 31 Mar 2022 12:32:09 -0300 Subject: [PATCH] refactor: get app environment from string content --- package.json | 2 +- src/Factories/AthennaFactory.ts | 12 +++++------- src/Utils/getAppEnvironment.ts | 26 ++++++++++++++++++++++++++ tests/Unit/IgniteTest.ts | 2 +- 4 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 src/Utils/getAppEnvironment.ts diff --git a/package.json b/package.json index 38f3dca..eb6973f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "1.0.7", + "version": "1.0.8", "description": "", "license": "MIT", "author": "João Lenon ", diff --git a/src/Factories/AthennaFactory.ts b/src/Factories/AthennaFactory.ts index 5b32be7..e8cab7d 100644 --- a/src/Factories/AthennaFactory.ts +++ b/src/Factories/AthennaFactory.ts @@ -7,11 +7,12 @@ * file that was distributed with this source code. */ +import { Path } from '@secjs/utils' import { Logger } from '@athenna/logger' import { normalize, parse } from 'path' import { Http, Router } from '@athenna/http' import { resolveEnvFile } from '@athenna/config' -import { Config as SecConfig, Path } from '@secjs/utils' +import { getAppEnvironment } from 'src/Utils/getAppEnvironment' import { ResolveClassExport } from 'src/Utils/ResolveClassExport' import { AthennaErrorHandler } from 'src/Utils/AthennaErrorHandler' @@ -24,14 +25,11 @@ export class AthennaFactory { AthennaFactory.resolveNodeTs(fileName) - const secConfig = new SecConfig() - - secConfig.load(Path.config(`app${AthennaFactory.extension}`)) - process.env.NODE_ENV = SecConfig.get('app.environment') + process.env.NODE_ENV = getAppEnvironment( + Path.config(`app${AthennaFactory.extension}`), + ) resolveEnvFile() - - secConfig.load(Path.config(`app${AthennaFactory.extension}`)) Config.load(Path.config()) AthennaFactory.logger = new Logger().channel('application', { diff --git a/src/Utils/getAppEnvironment.ts b/src/Utils/getAppEnvironment.ts new file mode 100644 index 0000000..20759f4 --- /dev/null +++ b/src/Utils/getAppEnvironment.ts @@ -0,0 +1,26 @@ +/** + * @athenna/core + * + * (c) João Lenon + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { File } from '@secjs/utils' + +export function getAppEnvironment(path: string): string { + let env = new File(path) + .loadSync() + .getContentSync() + .toString() + .split('environment:')[1] + .split(',')[0] + .trim() + + if (env.includes('process.env.NODE_ENV')) { + env = env.split('process.env.NODE_ENV')[1].replace('/||/g', '').trim() + } + + return process.env.NODE_ENV || env +} diff --git a/tests/Unit/IgniteTest.ts b/tests/Unit/IgniteTest.ts index 37394a0..0ed8598 100644 --- a/tests/Unit/IgniteTest.ts +++ b/tests/Unit/IgniteTest.ts @@ -26,7 +26,7 @@ describe('\n IgniteTest', () => { expect(Config.get('app.name')).toBe('Athenna') expect(Config.get('app.domain')).toBe('http://localhost:1335') - const { json, statusCode } = await app.getServer().inject({ + const { json, statusCode } = await app.request({ method: 'GET', url: '/healthcheck', })