lib/src/service/Config.ts

47 lines
1.4 KiB
TypeScript
Raw Normal View History

import {Singleton} from '../di'
2021-06-03 03:36:25 +00:00
import {CanonicalRecursive} from './CanonicalRecursive'
2021-03-03 00:57:41 +00:00
2021-03-25 13:50:13 +00:00
/**
* Canonical unit that loads configuration files from `app/configs`.
*/
2021-03-03 00:57:41 +00:00
@Singleton()
export class Config extends CanonicalRecursive {
protected appPath: string[] = ['configs']
2021-06-03 03:36:25 +00:00
protected suffix = '.config'
2021-06-03 03:36:25 +00:00
protected canonicalItem = 'config'
2021-03-25 13:50:13 +00:00
/** If true, all the unique configuration keys will be stored for debugging. */
2021-06-03 03:36:25 +00:00
protected recordConfigAccesses = false
2021-03-25 13:50:13 +00:00
/** Array of all unique accessed config keys, if `recordConfigAccesses` is true. */
2021-03-09 16:02:15 +00:00
protected accessedKeys: string[] = []
2021-03-09 00:07:55 +00:00
2021-06-03 03:36:25 +00:00
public async up(): Promise<void> {
2021-03-09 00:07:55 +00:00
await super.up()
if ( this.get('server.debug', false) ) {
Error.stackTraceLimit = Infinity
2021-03-09 16:02:15 +00:00
this.recordConfigAccesses = true
}
}
2021-06-03 03:36:25 +00:00
public async down(): Promise<void> {
2021-03-09 16:02:15 +00:00
await super.down()
if ( this.recordConfigAccesses && this.accessedKeys.length ) {
this.logging.debug(`The following configuration keys were accessed while this application was online:`)
this.accessedKeys.forEach(key => this.logging.debug(` - ${key}`))
2021-03-09 00:07:55 +00:00
}
}
2021-03-09 16:02:15 +00:00
2021-06-03 03:36:25 +00:00
public get(key: string, fallback?: unknown): any {
2021-03-09 16:02:15 +00:00
if ( this.recordConfigAccesses && !this.accessedKeys.includes(key) ) {
this.accessedKeys.push(key)
}
2021-06-03 03:36:25 +00:00
return super.get(key, fallback)
2021-03-09 16:02:15 +00:00
}
2021-03-03 00:57:41 +00:00
}