Skip to content

Commit

Permalink
Merge pull request #4 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat: ignore folder when it does not exist in providers
  • Loading branch information
jlenon7 authored Mar 30, 2022
2 parents 656e6e0 + 3a14ef5 commit 4590c32
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 66 deletions.
11 changes: 11 additions & 0 deletions container/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @athenna/core
*
* (c) João Lenon <lenon@athenna.io>
*
* 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'
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/core",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand All @@ -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",
Expand Down
72 changes: 36 additions & 36 deletions src/Factories/AthennaFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,45 @@
*/

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'

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[] = []
Expand Down Expand Up @@ -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<Http> {
Expand All @@ -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
}
}
8 changes: 3 additions & 5 deletions src/Providers/ControllerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 3 additions & 5 deletions src/Providers/MiddlewareProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/AthennaErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @secjs/core
* @athenna/core
*
* (c) João Lenon <lenon@secjs.com.br>
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
4 changes: 2 additions & 2 deletions src/Utils/ResolveClassExport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @secjs/core
* @athenna/core
*
* (c) João Lenon <lenon@secjs.com.br>
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down
22 changes: 22 additions & 0 deletions src/Utils/getAppFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @secjs/core
*
* (c) João Lenon <lenon@secjs.com.br>
*
* 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)')
)
}
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/Stubs/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,5 @@ export default {
| array.
|
*/
preloads: ['./start/routes'],
preloads: ['../routes/http'],
}
File renamed without changes.
7 changes: 0 additions & 7 deletions tests/Stubs/start/kernel.ts

This file was deleted.

8 changes: 4 additions & 4 deletions tests/Unit/IgniteTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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()
})
})

0 comments on commit 4590c32

Please sign in to comment.