Skip to content

Commit 0a06952

Browse files
authored
Merge pull request #49 from SecJS/feat/len-load-config-without-ext
feat: load file without extension
2 parents b7a3143 + ec13e16 commit 0a06952

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ config.load('example.ts')
331331
// You can also use safeLoad to not reload files that were already loaded
332332
config.safeLoad('app.ts') // Will just return without errors, but app.ts will not be reloaded.
333333

334+
// And you can also load an configuration file without extension.
335+
// Config class will verify the Env NODE_TS to get the .js or .ts extension file
336+
config.safeLoad('app')
337+
334338
console.log(Config.get('app.name')) // 'secjs'
335339
```
336340

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.5",
3+
"version": "1.7.6",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",

src/Classes/Config.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Config {
2525
keys.forEach(key => (config = config[key]))
2626
}
2727

28-
if (!config) config = defaultValue
28+
if (config === undefined) config = defaultValue
2929

3030
return config
3131
}
@@ -53,10 +53,13 @@ export class Config {
5353
throw new InternalServerException(content)
5454
}
5555

56-
if (Config.configs.get(name)) return
56+
if (Config.configs.has(name)) return
5757
if (base.includes('.map') || base.includes('.d.ts')) return
5858

59-
const file = new File(`${dir}/${base}`).loadSync()
59+
const fileExtension = process.env.NODE_TS === 'true' ? 'ts' : 'js'
60+
const fileBase = `${name}.${fileExtension}`
61+
62+
const file = new File(`${dir}/${fileBase}`).loadSync()
6063
const fileContent = file.getContentSync().toString()
6164

6265
if (
@@ -87,6 +90,6 @@ export class Config {
8790
}
8891

8992
Config.debug.log(`Loading ${name} configuration file`)
90-
Config.configs.set(name, require(`${dir}/${name}`).default)
93+
Config.configs.set(name, require(file.path).default)
9194
}
9295
}

tests/Classes/config.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ describe('\n Config Class', () => {
3333
expect(Config.get('test-ondemand.hello')).toBe(true)
3434
})
3535

36+
it('should be able to load configuration file without extension', async () => {
37+
const config = new Config()
38+
39+
config.load(Path.tests('stubs/no-extension'))
40+
41+
expect(Config.get('no-extension.extension')).toBe(false)
42+
})
43+
3644
it('should throw an error when file is trying to use Config.get() to get information from other config file but this config file is trying to use Config.get() to this same file', async () => {
3745
try {
3846
new Config().load(Path.tests('stubs/infinite-callA.ts'))

tests/stubs/no-extension.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @secjs/utils
3+
*
4+
* (c) João Lenon <lenon@secjs.com.br>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
export default {
11+
extension: false,
12+
}

0 commit comments

Comments
 (0)