diff --git a/container/index.ts b/container/index.ts new file mode 100644 index 0000000..d8715b6 --- /dev/null +++ b/container/index.ts @@ -0,0 +1,11 @@ +/** + * @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. + */ + +export * from './HttpRoute' +export * from './HttpServer' diff --git a/package.json b/package.json index aad4aa3..2b35cf6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "1.0.2", + "version": "1.0.3", "description": "", "license": "MIT", "author": "João Lenon ", @@ -9,9 +9,9 @@ "homepage": "https://github.com/AthennaIO/Core#readme", "scripts": { "build": "tsc --project tsconfig.json && tscpaths -p tsconfig.json -s . -o .", - "test": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing jest --verbose", - "test:debug": "npm run lint:fix && cross-env NODE_TS=true cross-env NODE_ENV=testing cross-env DEBUG=api:* jest --verbose", - "lint:fix": "eslint \"{src,tests}/**/*.ts\" --fix" + "test": "npm run lint:fix && cross-env NODE_TS=true jest --verbose", + "test:debug": "DEBUG=api:* && npm run test", + "lint:fix": "eslint \"{src,container,tests}/**/*.ts\" --fix" }, "keywords": [ "nodejs", diff --git a/src/Factories/AthennaFactory.ts b/src/Factories/AthennaFactory.ts index 884e905..5b32be7 100644 --- a/src/Factories/AthennaFactory.ts +++ b/src/Factories/AthennaFactory.ts @@ -8,10 +8,10 @@ */ import { Logger } from '@athenna/logger' -import { parse, normalize } from 'path' +import { normalize, parse } from 'path' import { Http, Router } from '@athenna/http' import { resolveEnvFile } from '@athenna/config' -import { Path, Config as SecConfig } from '@secjs/utils' +import { Config as SecConfig, Path } from '@secjs/utils' import { ResolveClassExport } from 'src/Utils/ResolveClassExport' import { AthennaErrorHandler } from 'src/Utils/AthennaErrorHandler' @@ -19,6 +19,34 @@ export class AthennaFactory { private static logger: Logger private static extension: '.js' | '.ts' + constructor(fileName: string) { + console.clear() + + AthennaFactory.resolveNodeTs(fileName) + + const secConfig = new SecConfig() + + secConfig.load(Path.config(`app${AthennaFactory.extension}`)) + process.env.NODE_ENV = SecConfig.get('app.environment') + + resolveEnvFile() + + secConfig.load(Path.config(`app${AthennaFactory.extension}`)) + Config.load(Path.config()) + + AthennaFactory.logger = new Logger().channel('application', { + formatterConfig: { + context: AthennaFactory.name, + }, + }) + + const providers = AthennaFactory.getProviders() + + AthennaFactory.registerProviders(providers) + AthennaFactory.bootProviders(providers) + AthennaFactory.preloadFiles() + } + private static getProviders() { const providers = Config.get('app.providers') const providersNormalized: any[] = [] @@ -48,39 +76,20 @@ export class AthennaFactory { preloads.forEach(preload => { preload = normalize(preload) - const { dir, name } = parse(Path.pwd(preload)) + const { dir, name } = parse(Path.config(preload)) AthennaFactory.logger.log(`Preloading ${preload} file`) require(`${dir}/${name}${this.extension}`) }) } - constructor(fileName: string) { - console.clear() - - AthennaFactory.resolveNodeTs(fileName) - - const secConfig = new SecConfig() - - secConfig.load(Path.config(`app${AthennaFactory.extension}`)) - process.env.NODE_ENV = SecConfig.get('app.environment') - - resolveEnvFile() - - secConfig.load(Path.config(`app${AthennaFactory.extension}`)) - Config.load(Path.config()) - - AthennaFactory.logger = new Logger().channel('application', { - formatterConfig: { - context: AthennaFactory.name, - }, - }) + private static resolveNodeTs(fileName: string) { + const { ext } = parse(fileName) - const providers = AthennaFactory.getProviders() + if (ext === '.ts') process.env.NODE_TS = 'true' + else process.env.NODE_TS = 'false' - AthennaFactory.registerProviders(providers) - AthennaFactory.bootProviders(providers) - AthennaFactory.preloadFiles() + AthennaFactory.extension = ext as any } async http(): Promise { @@ -100,13 +109,4 @@ export class AthennaFactory { return http } - - private static resolveNodeTs(fileName: string) { - const { ext } = parse(fileName) - - if (ext === '.ts') process.env.NODE_TS = 'true' - else process.env.NODE_TS = 'false' - - AthennaFactory.extension = ext as any - } } diff --git a/src/Providers/ControllerProvider.ts b/src/Providers/ControllerProvider.ts index a8b4a06..806bac1 100644 --- a/src/Providers/ControllerProvider.ts +++ b/src/Providers/ControllerProvider.ts @@ -7,16 +7,14 @@ * file that was distributed with this source code. */ -import { Folder, Path } from '@secjs/utils' +import { Path } from '@secjs/utils' import { ServiceProvider } from '@athenna/ioc' +import { getAppFiles } from 'src/Utils/getAppFiles' import { ResolveClassExport } from 'src/Utils/ResolveClassExport' export class ControllerProvider extends ServiceProvider { boot(): void { - const controllers = new Folder(Path.app('Http/Controllers')) - .loadSync() - // Get all .js and .ts files but not the .d.ts. - .getFilesByPattern('!(*.d)*.*(js|ts)') + const controllers = getAppFiles(Path.app('Http/Controllers')) controllers.forEach(File => { this.container.bind( diff --git a/src/Providers/MiddlewareProvider.ts b/src/Providers/MiddlewareProvider.ts index 8003cf1..1dc9605 100644 --- a/src/Providers/MiddlewareProvider.ts +++ b/src/Providers/MiddlewareProvider.ts @@ -7,16 +7,14 @@ * file that was distributed with this source code. */ -import { Folder, Path } from '@secjs/utils' +import { Path } from '@secjs/utils' import { ServiceProvider } from '@athenna/ioc' +import { getAppFiles } from 'src/Utils/getAppFiles' import { ResolveClassExport } from 'src/Utils/ResolveClassExport' export class MiddlewareProvider extends ServiceProvider { boot(): void { - const middlewares = new Folder(Path.app('Http/Middlewares')) - .loadSync() - // Get all .js and .ts files but not the .d.ts. - .getFilesByPattern('!(*.d)*.*(js|ts)') + const middlewares = getAppFiles(Path.app('Http/Middlewares')) middlewares.forEach(File => { this.container.bind( diff --git a/src/Utils/AthennaErrorHandler.ts b/src/Utils/AthennaErrorHandler.ts index e9ea8e1..387e67d 100644 --- a/src/Utils/AthennaErrorHandler.ts +++ b/src/Utils/AthennaErrorHandler.ts @@ -1,7 +1,7 @@ /** - * @secjs/core + * @athenna/core * - * (c) João Lenon + * (c) João Lenon * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Utils/ResolveClassExport.ts b/src/Utils/ResolveClassExport.ts index 0a8a215..43760ef 100644 --- a/src/Utils/ResolveClassExport.ts +++ b/src/Utils/ResolveClassExport.ts @@ -1,7 +1,7 @@ /** - * @secjs/core + * @athenna/core * - * (c) João Lenon + * (c) João Lenon * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Utils/getAppFiles.ts b/src/Utils/getAppFiles.ts new file mode 100644 index 0000000..7a08261 --- /dev/null +++ b/src/Utils/getAppFiles.ts @@ -0,0 +1,22 @@ +/** + * @secjs/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 { existsSync } from 'fs' +import { File, Folder } from '@secjs/utils' + +export function getAppFiles(path: string): File[] { + if (!existsSync(path)) return [] + + return ( + new Folder(path) + .loadSync() + // Get all .js and .ts files but not the .d.ts. + .getFilesByPattern('!(*.d)*.*(js|ts)') + ) +} diff --git a/tests/Stubs/.env.testing b/tests/Stubs/.env.test similarity index 100% rename from tests/Stubs/.env.testing rename to tests/Stubs/.env.test diff --git a/tests/Stubs/config/app.ts b/tests/Stubs/config/app.ts index dec54c6..f10146b 100644 --- a/tests/Stubs/config/app.ts +++ b/tests/Stubs/config/app.ts @@ -170,5 +170,5 @@ export default { | array. | */ - preloads: ['./start/routes'], + preloads: ['../routes/http'], } diff --git a/tests/Stubs/start/routes.ts b/tests/Stubs/routes/http.ts similarity index 100% rename from tests/Stubs/start/routes.ts rename to tests/Stubs/routes/http.ts diff --git a/tests/Stubs/start/kernel.ts b/tests/Stubs/start/kernel.ts deleted file mode 100644 index c9bbb4d..0000000 --- a/tests/Stubs/start/kernel.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Ignite } from '../../../src/Ignite' - -async function main() { - await new Ignite(__filename).httpServer() -} - -main().catch() diff --git a/tests/Unit/IgniteTest.ts b/tests/Unit/IgniteTest.ts index 770dbf9..37394a0 100644 --- a/tests/Unit/IgniteTest.ts +++ b/tests/Unit/IgniteTest.ts @@ -12,9 +12,9 @@ import { File, Folder, Path } from '@secjs/utils' describe('\n IgniteTest', () => { beforeAll(() => { - new File(Path.tests('Stubs/.env.testing')).loadSync().copySync(Path.pwd('.env.testing')) + new File(Path.tests('Stubs/.env.test')).loadSync().copySync(Path.pwd('.env.test')) new Folder(Path.tests('Stubs/config')).loadSync().copySync(Path.pwd('config')) - new Folder(Path.tests('Stubs/start')).loadSync().copySync(Path.pwd('start')) + new Folder(Path.tests('Stubs/routes')).loadSync().copySync(Path.pwd('routes')) }) it('should be able to ignite an Athenna http project', async () => { @@ -39,7 +39,7 @@ describe('\n IgniteTest', () => { afterAll(() => { new Folder(Path.pwd('config')).removeSync() - new Folder(Path.pwd('start')).removeSync() - new File(Path.pwd('.env.testing')).removeSync() + new Folder(Path.pwd('routes')).removeSync() + new File(Path.pwd('.env.test')).removeSync() }) })