|
|
|
import {Singleton, Inject} from '../di'
|
|
|
|
import {CanonicalRecursive} from './CanonicalRecursive'
|
|
|
|
import {Logging} from './Logging'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Canonical unit that loads configuration files from `app/configs`.
|
|
|
|
*/
|
|
|
|
@Singleton()
|
|
|
|
export class Config extends CanonicalRecursive {
|
|
|
|
@Inject()
|
|
|
|
protected readonly logging!: Logging
|
|
|
|
|
|
|
|
protected appPath: string[] = ['configs']
|
|
|
|
|
|
|
|
protected suffix = '.config.js'
|
|
|
|
|
|
|
|
protected canonicalItem = 'config'
|
|
|
|
|
|
|
|
/** If true, all the unique configuration keys will be stored for debugging. */
|
|
|
|
protected recordConfigAccesses = false
|
|
|
|
|
|
|
|
/** Array of all unique accessed config keys, if `recordConfigAccesses` is true. */
|
|
|
|
protected accessedKeys: string[] = []
|
|
|
|
|
|
|
|
public async up(): Promise<void> {
|
|
|
|
await super.up()
|
|
|
|
|
|
|
|
if ( this.get('server.debug', false) ) {
|
|
|
|
Error.stackTraceLimit = Infinity
|
|
|
|
this.recordConfigAccesses = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async down(): Promise<void> {
|
|
|
|
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}`))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public get(key: string, fallback?: unknown): any {
|
|
|
|
if ( this.recordConfigAccesses && !this.accessedKeys.includes(key) ) {
|
|
|
|
this.accessedKeys.push(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.get(key, fallback)
|
|
|
|
}
|
|
|
|
}
|