Skip to content

Commit b7a3143

Browse files
authored
Merge pull request #48 from SecJS/feat/len-add-safe-load-config
feat: add safeLoad method to config
2 parents 8438efc + 7c32b6f commit b7a3143

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ console.log(Config.get('database.database')) // 'secjs'
328328
// You can call load again and you will never lose the previous states
329329
config.load('example.ts')
330330

331+
// You can also use safeLoad to not reload files that were already loaded
332+
config.safeLoad('app.ts') // Will just return without errors, but app.ts will not be reloaded.
333+
331334
console.log(Config.get('app.name')) // 'secjs'
332335
```
333336

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/utils",
3-
"version": "1.7.4",
3+
"version": "1.7.5",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Classes/Config.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,20 @@ export class Config {
3030
return config
3131
}
3232

33-
// Load the configuration file on demand
33+
clear() {
34+
Config.configs.clear()
35+
}
36+
37+
safeLoad(path: string) {
38+
const { name } = parse(path)
39+
40+
if (Config.configs.has(name)) {
41+
return
42+
}
43+
44+
return this.load(path)
45+
}
46+
3447
load(path: string, callNumber = 0) {
3548
const { dir, name, base } = parse(path)
3649

@@ -76,8 +89,4 @@ export class Config {
7689
Config.debug.log(`Loading ${name} configuration file`)
7790
Config.configs.set(name, require(`${dir}/${name}`).default)
7891
}
79-
80-
clear() {
81-
Config.configs.clear()
82-
}
8392
}

0 commit comments

Comments
 (0)