Skip to content

Commit

Permalink
Merge pull request #162 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(root): use the right application root path
  • Loading branch information
jlenon7 authored Mar 26, 2023
2 parents bdb44f3 + 5a05823 commit 4f449c2
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 37 deletions.
3 changes: 0 additions & 3 deletions bin/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

import { assert } from '@japa/assert'
import { Config } from '@athenna/config'
import { Importer } from '@athenna/test'
import { specReporter } from '@japa/spec-reporter'
import { configure, processCliArgs, run } from '@japa/runner'
Expand Down Expand Up @@ -38,8 +37,6 @@ process.env.IS_TS = 'true'

process.env.CORE_TESTING = 'true'

Config.set('meta', import.meta.url)

/*
|--------------------------------------------------------------------------
| Configure tests
Expand Down
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/core",
"version": "3.3.3",
"version": "3.3.4",
"description": "The plug and play Node.js framework.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
50 changes: 30 additions & 20 deletions src/Ignite/Ignite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ export class Ignite {
bootLogs: true,
shutdownLogs: false,
loadConfigSafe: true,
athennaRcPath: '../.athennarc.json',
configPath: './config',
athennaRcPath: './.athennarc.json',
uncaughtExceptionHandler: this.handleError,
})

if (!isAbsolute(this.options.athennaRcPath)) {
this.options.athennaRcPath = resolve(
Module.createDirname(this.meta),
this.options.athennaRcPath,
)
}

this.setUncaughtExceptionHandler()
this.setApplicationRootPath()

this.options.envPath = this.resolvePath(this.options.envPath)
this.options.configPath = this.resolvePath(this.options.configPath)
this.options.athennaRcPath = this.resolvePath(this.options.athennaRcPath)

await this.setRcContentAndAppVars()
this.verifyNodeEngineVersion()
this.registerItselfToTheContainer()
Expand Down Expand Up @@ -314,16 +314,12 @@ export class Ignite {
*/
public async setRcContentAndAppVars() {
const file = new File(this.options.athennaRcPath, '')
const pkgJsonFile = new File(Path.pwd('package.json'), '')

const pkgJson = pkgJsonFile.fileExists
? await pkgJsonFile.getContentAsJson()
: {}
const pkgJson = await new File(
Path.originalPwd('package.json'),
).getContentAsJson()
const corePkgJson = await new File('../../package.json').getContentAsJson()
const coreSemverVersion = this.parseVersion(corePkgJson.version)

this.setApplicationRootPath()

process.env.APP_NAME = pkgJson.name
process.env.APP_VERSION = this.parseVersion(pkgJson.version).toString()
process.env.ATHENNA_VERSION = `Athenna Framework ${coreSemverVersion.toString()}`
Expand Down Expand Up @@ -360,6 +356,8 @@ export class Ignite {
...replaceableConfigs,
})

this.options.athennaRcPath = file.path

return
}

Expand All @@ -376,7 +374,7 @@ export class Ignite {
}

athennaRc.isInPackageJson = true
this.options.athennaRcPath = Path.pwd('package.json')
this.options.athennaRcPath = Path.originalPwd('package.json')

Config.set('rc', {
...athennaRc,
Expand Down Expand Up @@ -407,10 +405,7 @@ export class Ignite {
* ```
*/
public async setConfigurationFiles(): Promise<void> {
await Config.loadAll(
this.options.configPath || Path.config(),
this.options.loadConfigSafe,
)
await Config.loadAll(this.options.configPath, this.options.loadConfigSafe)
}

/**
Expand Down Expand Up @@ -463,4 +458,19 @@ export class Ignite {
},
}
}

/**
* Resolve some relative path from the root of the project.
*/
private resolvePath(path: string): string {
if (!path) {
return path
}

if (!isAbsolute(path)) {
return resolve(Path.pwd(), path)
}

return path
}
}
22 changes: 12 additions & 10 deletions src/Types/IgniteOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@ export type IgniteOptions = {
shutdownLogs?: boolean

/**
* Your environment variable file path. By default, Athenna will load
* your ".env" file (just to get the NODE_ENV env) and then try to
* reload with "OVERRIDE_ENV=true" searching for the ".env.${NODE_ENV}"
* file.
* Your environment variable file path. If the path is not set, Athenna will
* read your ".env" file (if exists) to get the NODE_ENV environment variable.
* Then it will load the ".env.${NODE_ENV}" file (if exists).
*
* If the envFile path is set, Athenna will only load
* the env path you set.
* If the envFile path is set, Athenna will only load the env path you set.
*
* @default Path.pwd('.env')
* @default undefined
*/
envPath?: string

/**
* The configuration files path.
*
* @default Path.config()
* @default './config'
*/
configPath?: string

Expand Down Expand Up @@ -71,9 +69,13 @@ export type IgniteOptions = {
* use the "athenna" property inside your package.json file, but the priority
* of Athenna load will always be the .athennarc.json file. So, if you wish to
* move your properties to the "athenna" property, you will need to delete the
* .athennarc.json file.
* .athennarc.json file. Also, if you use a relative path in this property such
* as '.athennarc.json', Athenna will resolve the path from your project root.
* Is the same of using Path.pwd('.athennarc.json'). We are not using Path.pwd
* helper here because if you run the application outside of the project root,
* the pwd path will not be from your application.
*
* @default Path.pwd('.athennarc.json')
* @default './.athennarc.json'
*/
athennaRcPath?: string

Expand Down
5 changes: 4 additions & 1 deletion tests/Helpers/BaseCommandTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
* file that was distributed with this source code.
*/

import { URL } from 'node:url'
import { Config } from '@athenna/config'
import { ViewProvider } from '@athenna/view'
import { Exec, File, Folder } from '@athenna/common'
import { LoggerProvider } from '@athenna/logger'
import { Exec, File, Folder } from '@athenna/common'
import { ExitFaker, AfterEach, BeforeEach } from '@athenna/test'
import { ConsoleKernel, ArtisanProvider, COMMANDS_SETTINGS, CommanderHandler } from '@athenna/artisan'

Expand All @@ -24,6 +25,8 @@ export class BaseCommandTest {

await Config.loadAll(Path.stubs('config'))

Config.set('meta', new URL('../../bin/test.ts', import.meta.url).href)

new ViewProvider().register()
new LoggerProvider().register()
new ArtisanProvider().register()
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Ignite/IgniteTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { Test, ExitFaker, TestContext } from '@athenna/test'
export default class IgniteTest extends BaseTest {
@Test()
public async shouldBeAbleToIgniteTheApplicationWhenInstantiatingIgnite({ assert }: TestContext) {
const cwd = process.cwd()
const ignite = await new Ignite().load(Config.get('meta'))

assert.equal(ignite.meta, Config.get('meta'))
assert.equal(Config.get('rc.callPath'), cwd)
assert.containsSubset(ignite.options, {
bootLogs: true,
shutdownLogs: false,
Expand Down

0 comments on commit 4f449c2

Please sign in to comment.