@@ -2,9 +2,7 @@ import logger from './utils/logger'
22
33import { parse } from 'path'
44import { Env } from '@secjs/env'
5- import { getFolders } from '@secjs/utils'
6- import { FileContract } from '@secjs/contracts'
7- import { getFoldersSync } from './utils/getFoldersSync'
5+ import { File , Folder , Path } from '@secjs/utils'
86import { InternalServerException } from '@secjs/exceptions'
97
108export class Config {
@@ -22,7 +20,7 @@ export class Config {
2220 Config . configs . clear ( )
2321 }
2422
25- static get < T > ( key : string , defaultValue = undefined ) : T {
23+ static get < T = any > ( key : string , defaultValue = undefined ) : T | undefined {
2624 const [ mainKey , ...keys ] = key . split ( '.' )
2725
2826 let config = this . configs . get ( mainKey )
@@ -44,23 +42,23 @@ export class Config {
4442 loadSync ( configPath = '/config' ) {
4543 Config . loadEnvs ( )
4644
47- const path = ` ${ process . cwd ( ) } ${ configPath } `
45+ const path = Path . pwd ( configPath )
4846
49- const { files } = getFoldersSync ( path , true )
47+ const { files } = new Folder ( path ) . loadSync ( { withFileContent : true } )
5048
51- files . forEach ( file => Config . loadOnDemand ( ` ${ path } / ${ file . name } ` , files , 0 ) )
49+ files . forEach ( file => Config . loadOnDemand ( file . path , files , 0 ) )
5250
5351 return this
5452 }
5553
5654 async load ( configPath = '/config' ) {
5755 Config . loadEnvs ( )
5856
59- const path = ` ${ process . cwd ( ) } ${ configPath } `
57+ const path = Path . pwd ( configPath )
6058
61- const { files } = await getFolders ( path , true )
59+ const { files } = await new Folder ( path ) . load ( { withFileContent : true } )
6260
63- files . forEach ( file => Config . loadOnDemand ( ` ${ path } / ${ file . name } ` , files , 0 ) )
61+ files . forEach ( file => Config . loadOnDemand ( file . path , files , 0 ) )
6462
6563 return this
6664 }
@@ -82,16 +80,12 @@ export class Config {
8280 } )
8381 }
8482
85- private static loadOnDemand (
86- path : string ,
87- files : FileContract [ ] ,
88- callNumber = 0 ,
89- ) {
83+ private static loadOnDemand ( path : string , files : File [ ] , callNumber = 0 ) {
9084 const { dir, name, base } = parse ( path )
9185
9286 if ( base . includes ( '.map' ) || base . includes ( '.d.ts' ) ) return
9387
94- const file = files . find ( file => file . name === base )
88+ const file = files . find ( file => file . base === base )
9589
9690 if ( ! file ) return
9791 if ( this . configs . get ( name ) ) return
@@ -102,8 +96,10 @@ export class Config {
10296 throw new InternalServerException ( content )
10397 }
10498
105- if ( typeof file . value === 'string' && file . value . includes ( 'Config.get' ) ) {
106- const matches = file . value . match ( / \( ( [ ^ ) ] + ) \) / g)
99+ const fileContent = file . getContentSync ( ) . toString ( )
100+
101+ if ( fileContent . includes ( 'Config.get' ) ) {
102+ const matches = fileContent . match ( / \( ( [ ^ ) ] + ) \) / g)
107103
108104 for ( const match of matches ) {
109105 if ( this . configs . get ( `env-${ match } ` ) ) continue
0 commit comments