Skip to content

Commit 405ef1d

Browse files
authored
Merge pull request #11 from SecJS/refactor/len-update-utils-type
refactor: Update secjs/utils
2 parents 4cd2bb3 + 9bd9f93 commit 405ef1d

File tree

5 files changed

+66
-110
lines changed

5 files changed

+66
-110
lines changed

package-lock.json

Lines changed: 45 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/config",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",
@@ -19,11 +19,11 @@
1919
"environment-variables"
2020
],
2121
"devDependencies": {
22-
"@secjs/contracts": "1.1.6",
23-
"@secjs/env": "1.2.3",
24-
"@secjs/exceptions": "1.0.3",
25-
"@secjs/logger": "1.2.1",
26-
"@secjs/utils": "1.3.8",
22+
"@secjs/contracts": "1.1.7",
23+
"@secjs/env": "1.2.5",
24+
"@secjs/exceptions": "1.0.4",
25+
"@secjs/logger": "1.2.2",
26+
"@secjs/utils": "1.4.3",
2727
"@types/debug": "4.1.5",
2828
"@types/jest": "27.0.1",
2929
"@types/node": "14.17.0",

src/Config.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import logger from './utils/logger'
22

33
import { parse } from 'path'
44
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'
86
import { InternalServerException } from '@secjs/exceptions'
97

108
export class Config {
@@ -22,7 +20,7 @@ export class Config {
2220
Config.configs.clear()
2321
}
2422

25-
static get<T>(key: string, defaultValue = undefined): T {
23+
static get<T = any>(key: string, defaultValue = undefined): T | undefined {
2624
const [mainKey, ...keys] = key.split('.')
2725

2826
let config = this.configs.get(mainKey)
@@ -44,23 +42,23 @@ export class Config {
4442
loadSync(configPath = '/config') {
4543
Config.loadEnvs()
4644

47-
const path = `${process.cwd()}${configPath}`
45+
const path = Path.pwd(configPath)
4846

49-
const { files } = getFoldersSync(path, true)
47+
const { files } = new Folder(path).loadSync({ withFileContent: true })
5048

51-
files.forEach(file => Config.loadOnDemand(`${path}/${file.name}`, files, 0))
49+
files.forEach(file => Config.loadOnDemand(file.path, files, 0))
5250

5351
return this
5452
}
5553

5654
async load(configPath = '/config') {
5755
Config.loadEnvs()
5856

59-
const path = `${process.cwd()}${configPath}`
57+
const path = Path.pwd(configPath)
6058

61-
const { files } = await getFolders(path, true)
59+
const { files } = await new Folder(path).load({ withFileContent: true })
6260

63-
files.forEach(file => Config.loadOnDemand(`${path}/${file.name}`, files, 0))
61+
files.forEach(file => Config.loadOnDemand(file.path, files, 0))
6462

6563
return this
6664
}
@@ -82,16 +80,12 @@ export class Config {
8280
})
8381
}
8482

85-
private static loadOnDemand(
86-
path: string,
87-
files: FileContract[],
88-
callNumber = 0,
89-
) {
83+
private static loadOnDemand(path: string, files: File[], callNumber = 0) {
9084
const { dir, name, base } = parse(path)
9185

9286
if (base.includes('.map') || base.includes('.d.ts')) return
9387

94-
const file = files.find(file => file.name === base)
88+
const file = files.find(file => file.base === base)
9589

9690
if (!file) return
9791
if (this.configs.get(name)) return
@@ -102,8 +96,10 @@ export class Config {
10296
throw new InternalServerException(content)
10397
}
10498

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)
107103

108104
for (const match of matches) {
109105
if (this.configs.get(`env-${match}`)) continue

src/utils/getFoldersSync.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/utils/global.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare global {
77
loadSync(configPath?: string): void
88
load(configPath?: string): Promise<void>
99
static verifyPath(folderName?: string): string
10-
static get<T>(key: string, defaultValue?: any): T
10+
static get<T = any>(key: string, defaultValue?: any): T | undefined
1111
}
1212
}
1313

0 commit comments

Comments
 (0)