Skip to content

Commit

Permalink
Merge pull request #5 from SecJS/feat/len-load-sync
Browse files Browse the repository at this point in the history
fix(config): add verifyPath method to Config class
  • Loading branch information
jlenon7 authored Nov 19, 2021
2 parents ce42578 + a599e7f commit 8292da8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ export class Config {
return this
}

static verifyPath(folderName = 'dist') {
if (process.env.NODE_ENV === 'testing') return `/${folderName}/config`

return '/config'
}

private static loadEnvs() {
// Important to load all env files in process.env
Env('NODE_ENV', '')
Expand All @@ -82,6 +88,9 @@ export class Config {
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)

if (!file) return
Expand All @@ -108,6 +117,6 @@ export class Config {
}

logger.debug(`Loading ${name} configuration file`)
this.configs.set(name, require(`${dir}/${base}`).default)
this.configs.set(name, require(`${dir}/${name}`).default)
}
}
5 changes: 3 additions & 2 deletions src/utils/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export {}

declare global {
class Config {
loadSync(configPath?: string): void
load(configPath?: string): Promise<void>
static verifyPath(folderName?: string): string
static get<T>(key: string, defaultValue?: any): T
load(): Promise<void>
loadSync(): void
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/global.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import '../src/utils/global'

new Config().loadSync()

describe('\n Config', () => {
it('should be able to use global Config instance', async () => {
expect(Config.get('app.hello')).toBe('world')
Expand Down

0 comments on commit 8292da8

Please sign in to comment.