File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11{
22 "name" : " @secjs/config" ,
3- "version" : " 1.1.1 " ,
3+ "version" : " 1.1.2 " ,
44 "description" : " " ,
55 "license" : " MIT" ,
66 "author" : " João Lenon" ,
Original file line number Diff line number Diff line change @@ -39,6 +39,10 @@ export class Config {
3939 return config
4040 }
4141
42+ loadIfUnloaded ( configPath = '/config' ) {
43+ if ( ! Config . configs . size ) this . loadSync ( configPath )
44+ }
45+
4246 loadSync ( configPath = '/config' ) {
4347 Config . loadEnvs ( )
4448
@@ -47,8 +51,6 @@ export class Config {
4751 const { files } = new Folder ( path ) . loadSync ( { withFileContent : true } )
4852
4953 files . forEach ( file => Config . loadOnDemand ( file . path , files , 0 ) )
50-
51- return this
5254 }
5355
5456 async load ( configPath = '/config' ) {
@@ -59,14 +61,6 @@ export class Config {
5961 const { files } = await new Folder ( path ) . load ( { withFileContent : true } )
6062
6163 files . forEach ( file => Config . loadOnDemand ( file . path , files , 0 ) )
62-
63- return this
64- }
65-
66- static verifyPath ( folderName = 'dist' ) {
67- if ( process . env . NODE_ENV === 'testing' ) return '/config'
68-
69- return `/${ folderName } /config`
7064 }
7165
7266 private static loadEnvs ( ) {
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ export {}
55
66declare global {
77 class Config {
8+ loadIfUnloaded ( configPath ?: string ) : void
89 loadSync ( configPath ?: string ) : void
910 load ( configPath ?: string ) : Promise < void >
1011 static verifyPath ( folderName ?: string ) : string
@@ -16,6 +17,6 @@ const _global = global as any
1617
1718Path . switchEnvVerify ( )
1819
19- new ConfigInstance ( ) . loadSync ( )
20+ new ConfigInstance ( ) . loadIfUnloaded ( )
2021
2122_global . Config = ConfigInstance
Original file line number Diff line number Diff line change @@ -80,4 +80,13 @@ describe('\n Config', () => {
8080 expect ( Config . get ( 'DB_NAME' ) ) . toBe ( 'testing' )
8181 expect ( Config . get ( 'database.dbName' ) ) . toBe ( 'testing' )
8282 } )
83+
84+ it ( 'should be able to use loadIfUnloaded method to load configs if they are not loaded' , async ( ) => {
85+ expect ( Config . get ( 'app' ) ) . toBe ( undefined )
86+
87+ new Config ( ) . loadIfUnloaded ( )
88+
89+ expect ( Config . get ( 'DB_NAME' ) ) . toBe ( 'testing' )
90+ expect ( Config . get ( 'database.dbName' ) ) . toBe ( 'testing' )
91+ } )
8392} )
You can’t perform that action at this time.
0 commit comments