From 3dbc746b7f61459f38507a858ac0b035b6950ee3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 18 Nov 2021 22:29:14 -0300 Subject: [PATCH 1/3] fix(config): add verifyPath method to Config class --- src/Config.ts | 6 ++++++ src/utils/global.ts | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Config.ts b/src/Config.ts index 7b79d22..cf8cac9 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -65,6 +65,12 @@ export class Config { return this } + private 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', '') diff --git a/src/utils/global.ts b/src/utils/global.ts index 0bfbd6b..ae60c88 100644 --- a/src/utils/global.ts +++ b/src/utils/global.ts @@ -5,8 +5,9 @@ export {} declare global { class Config { static get(key: string, defaultValue?: any): T - load(): Promise - loadSync(): void + load(configPath?: string): Promise + verifyPath(folderName?: string): string + loadSync(configPath?: string): void } } From 362eb39e83561fcdc00c9ad7d1e9e1759951d3c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 18 Nov 2021 22:32:29 -0300 Subject: [PATCH 2/3] fix(config): refactor to be static --- src/Config.ts | 2 +- src/utils/global.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Config.ts b/src/Config.ts index cf8cac9..a58f662 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -65,7 +65,7 @@ export class Config { return this } - private static verifyPath(folderName = 'dist') { + static verifyPath(folderName = 'dist') { if (process.env.NODE_ENV === 'testing') return `/${folderName}/config` return '/config' diff --git a/src/utils/global.ts b/src/utils/global.ts index ae60c88..e1d01e3 100644 --- a/src/utils/global.ts +++ b/src/utils/global.ts @@ -4,10 +4,10 @@ export {} declare global { class Config { - static get(key: string, defaultValue?: any): T - load(configPath?: string): Promise - verifyPath(folderName?: string): string loadSync(configPath?: string): void + load(configPath?: string): Promise + static verifyPath(folderName?: string): string + static get(key: string, defaultValue?: any): T } } From a599e7fb08b5cdc0edc7805b94d67071b1d0b305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Thu, 18 Nov 2021 22:40:36 -0300 Subject: [PATCH 3/3] fix(config): add loadSync on global tests --- src/Config.ts | 5 ++++- tests/global.spec.ts | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Config.ts b/src/Config.ts index a58f662..c9bf20c 100644 --- a/src/Config.ts +++ b/src/Config.ts @@ -88,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 @@ -114,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) } } diff --git a/tests/global.spec.ts b/tests/global.spec.ts index 06762fa..727a997 100644 --- a/tests/global.spec.ts +++ b/tests/global.spec.ts @@ -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')