Skip to content

Commit 0aff88b

Browse files
committed
feat: add loadIfUnloaded method to Config class
1 parent dcc2d07 commit 0aff88b

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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",

src/Config.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff 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() {

src/utils/global.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export {}
55

66
declare 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

1718
Path.switchEnvVerify()
1819

19-
new ConfigInstance().loadSync()
20+
new ConfigInstance().loadIfUnloaded()
2021

2122
_global.Config = ConfigInstance

tests/config.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

0 commit comments

Comments
 (0)