Skip to content

Commit

Permalink
Merge pull request #172 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(ignite): add environments option in Ignite.load method
  • Loading branch information
jlenon7 authored Apr 26, 2023
2 parents e3cee30 + 8d10d18 commit a1c0627
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 62 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/core",
"version": "3.8.0",
"version": "3.9.0",
"description": "The plug and play Node.js framework.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -64,7 +64,7 @@
"devDependencies": {
"@athenna/artisan": "^3.9.0",
"@athenna/common": "^3.6.0",
"@athenna/config": "^3.6.0",
"@athenna/config": "^3.7.1",
"@athenna/http": "^3.10.0",
"@athenna/ioc": "^3.4.0",
"@athenna/logger": "^3.4.0",
Expand Down
11 changes: 0 additions & 11 deletions src/Applications/Artisan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@ import { Module, Options } from '@athenna/common'
import { ArtisanOptions } from '#src/Types/ArtisanOptions'

export class Artisan {
/**
* Load the necessary stuff before booting Artisan.
*/
public static async load() {
const { ViewProvider } = await import('@athenna/view')
const { ArtisanProvider } = await import('@athenna/artisan')

new ViewProvider().register()
new ArtisanProvider().register()
}

/**
* Boot the Artisan application and execute the commands of argv.
*/
Expand Down
27 changes: 15 additions & 12 deletions src/Ignite/Ignite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class Ignite {
beforePath: '',
bootLogs: true,
shutdownLogs: true,
environments: [],
loadConfigSafe: true,
athennaRcPath: './.athennarc.json',
uncaughtExceptionHandler: this.handleError,
Expand Down Expand Up @@ -88,11 +89,9 @@ export class Ignite {
* Ignite the REPL application.
*/
public async repl(): Promise<PrettyREPLServer> {
const environments = Config.get<string[]>('rc.environments', [])
this.options.environments.push('repl')

environments.push('repl')

await this.fire(environments)
await this.fire()

this.options.uncaughtExceptionHandler = async error => {
if (!Is.Exception(error)) {
Expand All @@ -116,7 +115,13 @@ export class Ignite {
* Ignite the Artisan application.
*/
public async artisan(argv: string[], options?: ArtisanOptions) {
await Artisan.load()
const { ViewProvider } = await import('@athenna/view')
const { ArtisanProvider } = await import('@athenna/artisan')

new ViewProvider().register()
new ArtisanProvider().register()

this.options.environments.push('console')

return Artisan.boot(argv, options)
}
Expand All @@ -125,11 +130,9 @@ export class Ignite {
* Ignite the Http server application.
*/
public async httpServer(options?: HttpOptions) {
const environments = Config.get<string[]>('rc.environments', [])
this.options.environments.push('http')

environments.push('http')

await this.fire(environments)
await this.fire()

return Http.boot(options)
}
Expand All @@ -138,13 +141,13 @@ export class Ignite {
* Fire the application configuring the env variables file, configuration files
* providers and preload files.
*/
public async fire(environments: string[]) {
public async fire() {
try {
Config.set('rc.environments', environments)

this.setEnvVariablesFile()
await this.setConfigurationFiles()

Config.push('rc.environments', this.options.environments)

await LoadHelper.regootProviders()
await LoadHelper.preloadFiles()
} catch (err) {
Expand Down
12 changes: 10 additions & 2 deletions src/Types/IgniteOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ export type IgniteOptions = {
envPath?: string

/**
* Load the configurations file safelly. If this option is true, Athenna
* The environment where the ignite application will run. Value in here will
* be merged with default environments applied by Athenna.
*
* @default []
*/
environments?: string[]

/**
* Load the configurations file safely. If this option is true, Athenna
* will not reload configuration files that are already loaded.
*
* @default false
*/
loadConfigSafe?: boolean

/**
* The before path that will be used in all "Path" helper calls. This is extremelly
* The before path that will be used in all "Path" helper calls. This is extremely
* useful when working with TypeScript and you need to build your code, just set
* the "beforePath" as "/build" and Athenna will automatically add it if running
* ".js" files.
Expand Down
30 changes: 17 additions & 13 deletions tests/Unit/Applications/ArtisanTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { fake } from 'sinon'
import { Log } from '@athenna/logger'
import { Config } from '@athenna/config'
import { File, Path } from '@athenna/common'
import { ViewProvider } from '@athenna/view'
import { ArtisanProvider } from '@athenna/artisan'
import { BaseTest } from '#tests/Helpers/BaseTest'
import { Artisan } from '#src/Applications/Artisan'
import { CALLED_MAP } from '#tests/Helpers/CalledMap'
Expand All @@ -19,17 +21,11 @@ import { ConsoleKernel } from '#tests/Stubs/kernels/ConsoleKernel'
import { ConsoleExceptionHandler } from '#tests/Stubs/handlers/ConsoleExceptionHandler'

export default class ArtisanTest extends BaseTest {
@Test()
public async shouldBeAbleToLoadArtisanApplicationDefaults({ assert }: TestContext) {
await Artisan.load()

assert.isTrue(ioc.hasDependency('Athenna/Core/View'))
assert.isTrue(ioc.hasDependency('Athenna/Core/Artisan'))
}

@Test()
public async shouldBeAbleToBootAnArtisanApplicationWithoutAnyOption({ assert }: TestContext) {
await Artisan.load()
new ViewProvider().register()
new ArtisanProvider().register()

await Artisan.boot(['node', 'artisan'])

assert.isTrue(ExitFaker.faker.called)
Expand All @@ -48,7 +44,9 @@ export default class ArtisanTest extends BaseTest {
.withArgs('application')
.returns({ success: args => successFake(args) })

await Artisan.load()
new ViewProvider().register()
new ArtisanProvider().register()

await Artisan.boot(['node', 'artisan'])

assert.isTrue(ExitFaker.faker.called)
Expand All @@ -58,7 +56,9 @@ export default class ArtisanTest extends BaseTest {

@Test()
public async shouldBeAbleToBootAnArtisanApplicationAndRegisterCommandsFromRoutes({ assert }: TestContext) {
await Artisan.load()
new ViewProvider().register()
new ArtisanProvider().register()

await Artisan.boot(['node', 'artisan', 'test:generate'], {
displayName: null,
routePath: Path.stubs('routes/console.ts'),
Expand All @@ -70,7 +70,9 @@ export default class ArtisanTest extends BaseTest {

@Test()
public async shouldBeAbleToBootAnArtisanApplicationAndRegisterAConsoleKernel({ assert }: TestContext) {
await Artisan.load()
new ViewProvider().register()
new ArtisanProvider().register()

await Artisan.boot(['node', 'artisan', 'test:generate'], {
displayName: null,
routePath: Path.stubs('routes/console.ts'),
Expand All @@ -84,7 +86,9 @@ export default class ArtisanTest extends BaseTest {

@Test()
public async shouldBeAbleToBootAnArtisanApplicationAndRegisterAConsoleExceptionHandler({ assert }: TestContext) {
await Artisan.load()
new ViewProvider().register()
new ArtisanProvider().register()

await Artisan.boot(['node', 'artisan', 'test:generate'], {
displayName: null,
routePath: Path.stubs('routes/console.ts'),
Expand Down
37 changes: 24 additions & 13 deletions tests/Unit/Ignite/IgniteTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,15 @@ export default class IgniteTest extends BaseTest {

const ignite = await new Ignite().load(Config.get('meta'), {
envPath: Path.stubs('.env'),
environments: ['console'],
})

await ignite.fire(['console'])
await ignite.fire()

assert.equal(Env('IGNITE_FIRED'), true)
assert.equal(Env('NODE_ENV'), 'local')
assert.isTrue(Config.is('ignite.fired', true))
assert.deepEqual(Config.get('rc.environments'), ['console'])
assert.deepEqual(Config.get('rc.environments'), ['other', 'console'])
assert.deepEqual(Config.get('rc.providers'), [
'#src/Providers/CoreProvider',
'@athenna/http/providers/HttpRouteProvider',
Expand All @@ -135,14 +136,16 @@ export default class IgniteTest extends BaseTest {
Config.set('rc.environments', ['other'])
Config.set('rc.directories', { config: 'tests/Stubs/igniteConfig' })

const ignite = await new Ignite().load(Config.get('meta'))
const ignite = await new Ignite().load(Config.get('meta'), {
environments: ['console'],
})

await ignite.fire(['console'])
await ignite.fire()

assert.isUndefined(Env('IGNITE_FIRED'))
assert.equal(Env('ENV_LOCAL_LOADED'), true)
assert.isTrue(Config.is('ignite.fired', true))
assert.deepEqual(Config.get('rc.environments'), ['console'])
assert.deepEqual(Config.get('rc.environments'), ['other', 'console'])
assert.deepEqual(Config.get('rc.providers'), [
'#src/Providers/CoreProvider',
'@athenna/http/providers/HttpRouteProvider',
Expand All @@ -154,9 +157,11 @@ export default class IgniteTest extends BaseTest {
public async shouldBeAbleToHandleSyntaxErrorExceptionsOfConfigsUsingTheDefaultIgniteHandler({ assert }: TestContext) {
Config.set('rc.directories', { config: 'tests/Stubs/syntaxErrorConfig' })

const ignite = await new Ignite().load(Config.get('meta'))
const ignite = await new Ignite().load(Config.get('meta'), {
environments: ['console'],
})

await ignite.fire(['console'])
await ignite.fire()

assert.isTrue(ExitFaker.faker.calledWith(1))
}
Expand Down Expand Up @@ -219,9 +224,11 @@ export default class IgniteTest extends BaseTest {

@Test()
public async shouldBeAbleToExecuteDefaultSIGINTSignalOfIgnite({ assert }: TestContext) {
const ignite = await new Ignite().load(Config.get('meta'))
const ignite = await new Ignite().load(Config.get('meta'), {
environments: ['console'],
})

await ignite.fire(['console'])
await ignite.fire()

process.emit('SIGINT')

Expand All @@ -234,9 +241,11 @@ export default class IgniteTest extends BaseTest {
process.kill = processKillStub
Config.set('rc.providers', ['#tests/Stubs/providers/ConsoleEnvProvider'])

const ignite = await new Ignite().load(Config.get('meta'))
const ignite = await new Ignite().load(Config.get('meta'), {
environments: ['console'],
})

await ignite.fire(['console'])
await ignite.fire()

process.emit('SIGTERM', 'SIGTERM')

Expand Down Expand Up @@ -294,9 +303,11 @@ export default class IgniteTest extends BaseTest {

@Test()
public async shouldBeAbleToRegisterTheServicesDepsUsingTheCoreProvider({ assert }: TestContext) {
const ignite = await new Ignite().load(Config.get('meta'))
const ignite = await new Ignite().load(Config.get('meta'), {
environments: ['console'],
})

await ignite.fire(['console'])
await ignite.fire()

assert.isTrue(ioc.hasDependency('welcomeService'))
assert.isTrue(ioc.hasDependency('App/Services/WelcomeService'))
Expand Down

0 comments on commit a1c0627

Please sign in to comment.