@@ -2,9 +2,7 @@ import logger from './utils/logger'
2
2
3
3
import { parse } from 'path'
4
4
import { 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'
8
6
import { InternalServerException } from '@secjs/exceptions'
9
7
10
8
export class Config {
@@ -22,7 +20,7 @@ export class Config {
22
20
Config . configs . clear ( )
23
21
}
24
22
25
- static get < T > ( key : string , defaultValue = undefined ) : T {
23
+ static get < T = any > ( key : string , defaultValue = undefined ) : T | undefined {
26
24
const [ mainKey , ...keys ] = key . split ( '.' )
27
25
28
26
let config = this . configs . get ( mainKey )
@@ -44,23 +42,23 @@ export class Config {
44
42
loadSync ( configPath = '/config' ) {
45
43
Config . loadEnvs ( )
46
44
47
- const path = ` ${ process . cwd ( ) } ${ configPath } `
45
+ const path = Path . pwd ( configPath )
48
46
49
- const { files } = getFoldersSync ( path , true )
47
+ const { files } = new Folder ( path ) . loadSync ( { withFileContent : true } )
50
48
51
- files . forEach ( file => Config . loadOnDemand ( ` ${ path } / ${ file . name } ` , files , 0 ) )
49
+ files . forEach ( file => Config . loadOnDemand ( file . path , files , 0 ) )
52
50
53
51
return this
54
52
}
55
53
56
54
async load ( configPath = '/config' ) {
57
55
Config . loadEnvs ( )
58
56
59
- const path = ` ${ process . cwd ( ) } ${ configPath } `
57
+ const path = Path . pwd ( configPath )
60
58
61
- const { files } = await getFolders ( path , true )
59
+ const { files } = await new Folder ( path ) . load ( { withFileContent : true } )
62
60
63
- files . forEach ( file => Config . loadOnDemand ( ` ${ path } / ${ file . name } ` , files , 0 ) )
61
+ files . forEach ( file => Config . loadOnDemand ( file . path , files , 0 ) )
64
62
65
63
return this
66
64
}
@@ -82,16 +80,12 @@ export class Config {
82
80
} )
83
81
}
84
82
85
- private static loadOnDemand (
86
- path : string ,
87
- files : FileContract [ ] ,
88
- callNumber = 0 ,
89
- ) {
83
+ private static loadOnDemand ( path : string , files : File [ ] , callNumber = 0 ) {
90
84
const { dir, name, base } = parse ( path )
91
85
92
86
if ( base . includes ( '.map' ) || base . includes ( '.d.ts' ) ) return
93
87
94
- const file = files . find ( file => file . name === base )
88
+ const file = files . find ( file => file . base === base )
95
89
96
90
if ( ! file ) return
97
91
if ( this . configs . get ( name ) ) return
@@ -102,8 +96,10 @@ export class Config {
102
96
throw new InternalServerException ( content )
103
97
}
104
98
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)
107
103
108
104
for ( const match of matches ) {
109
105
if ( this . configs . get ( `env-${ match } ` ) ) continue
0 commit comments