Skip to content

Commit

Permalink
Merge pull request #13 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(path): adjust resolveEnvironment method
  • Loading branch information
jlenon7 authored Jan 15, 2023
2 parents a4310a3 + 8d1dc8b commit 73a6e53
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
16 changes: 13 additions & 3 deletions src/Helpers/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
21 changes: 19 additions & 2 deletions tests/Unit/PathTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '')
Expand Down Expand Up @@ -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 }) => {
Expand Down

0 comments on commit 73a6e53

Please sign in to comment.