From 8d1dc8bf9acbdeee7dd334c45132a7eb1f6f4dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Sun, 15 Jan 2023 11:44:43 -0300 Subject: [PATCH] fix(path): adjust resolveEnvironment method --- package-lock.json | 4 ++-- package.json | 2 +- src/Helpers/Path.js | 16 +++++++++++++--- src/index.d.ts | 16 ++++++++++++++++ tests/Unit/PathTest.js | 21 +++++++++++++++++++-- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index b8d25d7..98b335c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "3.0.1", + "version": "3.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "3.0.1", + "version": "3.0.2", "license": "MIT", "dependencies": { "@fastify/formbody": "7.4.0", diff --git a/package.json b/package.json index 90bb873..56a22ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "3.0.1", + "version": "3.0.2", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", diff --git a/src/Helpers/Path.js b/src/Helpers/Path.js index 1fe0793..6606f89 100644 --- a/src/Helpers/Path.js +++ b/src/Helpers/Path.js @@ -34,11 +34,11 @@ export class Path { * @param beforePath {string} * @return {typeof Path} */ - static resolveEnvironment(metaUrl, beforePath = '/build') { + static resolveEnvironment(metaUrl, beforePath = '') { const isTs = metaUrl.endsWith('.ts') ? 'true' : 'false' - process.env.IS_TS = isTs - this.defaultBeforePath = isTs === 'true' ? beforePath : '' + process.env.IS_TS = process.env.IS_TS || isTs + this.defaultBeforePath = process.env.IS_TS === 'true' ? '' : beforePath return this } @@ -289,6 +289,16 @@ export class Path { return this.app('Services' + sep + normalize(subPath)) } + /** + * Return the repositories' path of your project. + * + * @param {string} subPath + * @return {string} + */ + static repositories(subPath = sep) { + return this.app('Repositories' + sep + normalize(subPath)) + } + /** * Return the migrations' path of your project. * diff --git a/src/index.d.ts b/src/index.d.ts index fd007ef..f9c4071 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -2911,6 +2911,14 @@ export declare class Path { */ static services(subPath?: string): string + /** + * Return the repositories' path of your project. + * + * @param {string} subPath + * @return {string} + */ + static repositories(subPath?: string): string + /** * Return the migrations' path of your project. * @@ -3409,6 +3417,14 @@ declare global { */ static services(subPath?: string): string + /** + * Return the repositories' path of your project. + * + * @param {string} subPath + * @return {string} + */ + static repositories(subPath?: string): string + /** * Return the migrations' path of your project. * diff --git a/tests/Unit/PathTest.js b/tests/Unit/PathTest.js index 4374c76..ec1a9a4 100644 --- a/tests/Unit/PathTest.js +++ b/tests/Unit/PathTest.js @@ -12,12 +12,28 @@ import { test } from '@japa/runner' test.group('PathTest', () => { test('should be able to resolve the environment where the app will run', async ({ assert }) => { - Path.resolveEnvironment(import.meta.url.replace('.js', '.ts')) + const metaUrl = import.meta.url + const metaUrlTs = metaUrl.replace('.js', '.ts') + + Path.resolveEnvironment(metaUrlTs) + + assert.equal(process.env.IS_TS, 'true') + assert.equal(Path.defaultBeforePath, '') + + delete process.env.IS_TS + Path.resolveEnvironment(metaUrlTs, '/build') assert.equal(process.env.IS_TS, 'true') + assert.equal(Path.defaultBeforePath, '') + + delete process.env.IS_TS + Path.resolveEnvironment(metaUrl, '/build') + + assert.equal(process.env.IS_TS, 'false') assert.equal(Path.defaultBeforePath, '/build') - Path.resolveEnvironment(import.meta.url) + delete process.env.IS_TS + Path.resolveEnvironment(metaUrl) assert.equal(process.env.IS_TS, 'false') assert.equal(Path.defaultBeforePath, '') @@ -81,6 +97,7 @@ test.group('PathTest', () => { assert.equal(Path.http(), mainPath.concat(sep, 'Http')) assert.equal(Path.console(), mainPath.concat(sep, 'Console')) assert.equal(Path.services(), mainPath.concat(sep, 'Services')) + assert.equal(Path.repositories(), mainPath.concat(sep, 'Repositories')) }) test('should get the sub paths of database main path', async ({ assert }) => {