Skip to content

Commit

Permalink
feat(grease): createGreaser
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Nov 6, 2023
1 parent ad59ade commit 569291f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/__tests__/grease.create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Unit Tests - createGreaser
* @module grease/tests/unit/createGreaser
*/

import testSubject from '../grease.create'
import GreaseService from '../grease.service'

describe('unit:createGreaser', () => {
it('should return grease runner instance', async () => {
expect(await testSubject()).to.be.instanceof(GreaseService)
})
})
41 changes: 41 additions & 0 deletions src/grease.create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @file createGreaser
* @module grease/create
*/

import { LoggerService, UserLogLevel } from '#src/log'
import type { INestApplicationContext as App } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import GreaseModule from './grease.module'
import GreaseService from './grease.service'

/**
* Create a greaser.
*
* @see {@linkcode GreaseService}
*
* @async
*
* @return {Promise<GreaseService>} Grease runner instance
*/
const createGreaser = async (): Promise<GreaseService> => {
/**
* NestJS application context.
*
* @const {App} app
*/
const app: App = await NestFactory.createApplicationContext(GreaseModule, {
abortOnError: false,
autoFlushLogs: false,
bufferLogs: true
})

// use custom logger
app.useLogger(app.get(LoggerService).withTag('nest'))
app.useLogger([UserLogLevel.WARN])
app.flushLogs()

return (await app.init()).get(GreaseService)
}

export default createGreaser
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export * from './decorators'
export * from './enums'
export { default as createGreaser } from './grease.create'
export { default as GreaseModule } from './grease.module'
export { default as GreaseService } from './grease.service'
export type * from './interfaces'
Expand Down
15 changes: 15 additions & 0 deletions src/subdomains/log/providers/__tests__/logger.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ describe('unit:log/providers/LoggerService', () => {
})
})

describe('#setLogLevels', () => {
it('should return this log level', () => {
// Arrange
const cases: [keyof typeof LogLevel, UserLogLevel[]][] = [
['LOG', []],
['WARN', [UserLogLevel.ERROR, UserLogLevel.FATAL, UserLogLevel.WARN]]
]

// Act + Expect
cases.forEach(([key, levels]) => {
expect(new TestSubject().setLogLevels(levels)).to.equal(LogLevel[key])
})
})
})

describe('#sync', () => {
let subject: TestSubject

Expand Down
20 changes: 20 additions & 0 deletions src/subdomains/log/providers/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Reporter } from '#src/log/reporters'
import type { Colors } from '#src/log/types'
import type { GlobalOptions } from '#src/options'
import {
at,
define,
fallback,
ifelse,
Expand All @@ -24,6 +25,7 @@ import {
omit,
select,
set,
sort,
template,
uppercase,
values,
Expand Down Expand Up @@ -272,6 +274,24 @@ class LoggerService implements ILogger {
})
}

/**
* Set `this` log level to the highest level in the given log level array.
*
* @public
*
* @param {OrLowercase<UserLogLevel>[]} levels - Log levels
* @return {LogLevel} `this` log level
*/
public setLogLevels(levels: OrLowercase<UserLogLevel>[]): LogLevel {
if (levels.length) {
this.level = at(sort(levels, (level1, level2) => {
return LogLevel[uppercase(level2)] - LogLevel[uppercase(level1)]
}), 0)
}

return this.level
}

/**
* Write a `start` log.
*
Expand Down

0 comments on commit 569291f

Please sign in to comment.