Skip to content

Commit

Permalink
Merge pull request #11 from SecJS/refactor/len-update-utils-type
Browse files Browse the repository at this point in the history
refactor: Update secjs/utils
  • Loading branch information
jlenon7 authored Nov 27, 2021
2 parents 4cd2bb3 + 9bd9f93 commit 405ef1d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 110 deletions.
84 changes: 45 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@secjs/config",
"version": "1.0.6",
"version": "1.0.7",
"description": "",
"license": "MIT",
"author": "João Lenon",
Expand All @@ -19,11 +19,11 @@
"environment-variables"
],
"devDependencies": {
"@secjs/contracts": "1.1.6",
"@secjs/env": "1.2.3",
"@secjs/exceptions": "1.0.3",
"@secjs/logger": "1.2.1",
"@secjs/utils": "1.3.8",
"@secjs/contracts": "1.1.7",
"@secjs/env": "1.2.5",
"@secjs/exceptions": "1.0.4",
"@secjs/logger": "1.2.2",
"@secjs/utils": "1.4.3",
"@types/debug": "4.1.5",
"@types/jest": "27.0.1",
"@types/node": "14.17.0",
Expand Down
32 changes: 14 additions & 18 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import logger from './utils/logger'

import { parse } from 'path'
import { Env } from '@secjs/env'
import { getFolders } from '@secjs/utils'
import { FileContract } from '@secjs/contracts'
import { getFoldersSync } from './utils/getFoldersSync'
import { File, Folder, Path } from '@secjs/utils'
import { InternalServerException } from '@secjs/exceptions'

export class Config {
Expand All @@ -22,7 +20,7 @@ export class Config {
Config.configs.clear()
}

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

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

const path = `${process.cwd()}${configPath}`
const path = Path.pwd(configPath)

const { files } = getFoldersSync(path, true)
const { files } = new Folder(path).loadSync({ withFileContent: true })

files.forEach(file => Config.loadOnDemand(`${path}/${file.name}`, files, 0))
files.forEach(file => Config.loadOnDemand(file.path, files, 0))

return this
}

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

const path = `${process.cwd()}${configPath}`
const path = Path.pwd(configPath)

const { files } = await getFolders(path, true)
const { files } = await new Folder(path).load({ withFileContent: true })

files.forEach(file => Config.loadOnDemand(`${path}/${file.name}`, files, 0))
files.forEach(file => Config.loadOnDemand(file.path, files, 0))

return this
}
Expand All @@ -82,16 +80,12 @@ export class Config {
})
}

private static loadOnDemand(
path: string,
files: FileContract[],
callNumber = 0,
) {
private static loadOnDemand(path: string, files: File[], callNumber = 0) {
const { dir, name, base } = parse(path)

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

const file = files.find(file => file.name === base)
const file = files.find(file => file.base === base)

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

if (typeof file.value === 'string' && file.value.includes('Config.get')) {
const matches = file.value.match(/\(([^)]+)\)/g)
const fileContent = file.getContentSync().toString()

if (fileContent.includes('Config.get')) {
const matches = fileContent.match(/\(([^)]+)\)/g)

for (const match of matches) {
if (this.configs.get(`env-${match}`)) continue
Expand Down
46 changes: 0 additions & 46 deletions src/utils/getFoldersSync.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare global {
loadSync(configPath?: string): void
load(configPath?: string): Promise<void>
static verifyPath(folderName?: string): string
static get<T>(key: string, defaultValue?: any): T
static get<T = any>(key: string, defaultValue?: any): T | undefined
}
}

Expand Down

0 comments on commit 405ef1d

Please sign in to comment.