configManager now supports logging via tslog / cleanup / loglevel 0 in devmode 3 in prod
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import fs from 'node:fs';
|
||||
import _ from 'lodash';
|
||||
import { randomUUID, randomBytes } from 'crypto';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { Logger } from 'tslog';
|
||||
|
||||
export type configObject = Record<any, any>;
|
||||
|
||||
@ -17,18 +18,25 @@ export default class config {
|
||||
global: configObject;
|
||||
replaceSecrets: boolean;
|
||||
|
||||
#logger: Logger<unknown> | typeof console;
|
||||
|
||||
/**
|
||||
* Creates an instance of config.
|
||||
*
|
||||
* @constructor
|
||||
* @param {string} configPath Path to config file.
|
||||
* @param {boolean} replaceSecrets Whether to replace secrets with generated values.
|
||||
* @param {object} configPreset Default config object with default values.
|
||||
* @param {Logger<unknown> | typeof console} [logger] Optional (tslog) logger.
|
||||
*/
|
||||
constructor(configPath: string, replaceSecrets: boolean, configPreset: object) {
|
||||
constructor(configPath: string, replaceSecrets: boolean, configPreset: object, logger?: Logger<unknown> | typeof console) {
|
||||
this.#configPath = configPath;
|
||||
this.global = configPreset;
|
||||
this.replaceSecrets = replaceSecrets;
|
||||
|
||||
this.#logger = logger ?? console;
|
||||
|
||||
this.#logger.info(`Initializing config manager with path: ${this.#configPath}`);
|
||||
try {
|
||||
// Read config
|
||||
const data = fs.readFileSync(this.#configPath, 'utf8');
|
||||
@ -40,11 +48,11 @@ export default class config {
|
||||
} catch (err: any) {
|
||||
// If file does not exist, create it.
|
||||
if (err.code === 'ENOENT') {
|
||||
console.log(`Config file does not exist. Creating it at ${this.#configPath} now.`);
|
||||
this.#logger.info(`Config file does not exist. Creating it at ${this.#configPath} now.`);
|
||||
this.save_config();
|
||||
return;
|
||||
}
|
||||
console.error(`Could not read config file at ${this.#configPath} due to: ${err}`);
|
||||
this.#logger.error(`Could not read config file at ${this.#configPath} due to: ${err}`);
|
||||
// Exit process.
|
||||
process.exit(1);
|
||||
}
|
||||
@ -58,15 +66,15 @@ export default class config {
|
||||
// If enabled replace tokens defines as "gen" with random token
|
||||
if (this.replaceSecrets) {
|
||||
// Replace tokens with value "gen"
|
||||
this.generate_secrets(this.global, 'gen')
|
||||
this.generate_secrets(this.global, 'gen');
|
||||
}
|
||||
|
||||
fs.writeFileSync(this.#configPath, JSON.stringify(this.global, null, 8));
|
||||
} catch (err) {
|
||||
console.error(`Could not write config file at ${this.#configPath} due to: ${err}`);
|
||||
this.#logger.error(`Could not write config file at ${this.#configPath} due to: ${err}`);
|
||||
return;
|
||||
}
|
||||
console.log(`Successfully written config file to ${this.#configPath}`);
|
||||
this.#logger.info(`Successfully written config file to ${this.#configPath}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,11 +85,10 @@ export default class config {
|
||||
generate_secrets(obj: configObject, placeholder: string) {
|
||||
const stack = [obj];
|
||||
while (stack?.length > 0) {
|
||||
const currentObj:any = stack.pop();
|
||||
const currentObj: any = stack.pop();
|
||||
Object.keys(currentObj).forEach((key) => {
|
||||
|
||||
if (currentObj[key] === placeholder) {
|
||||
console.log('Generating secret: ' + key);
|
||||
this.#logger.info('Generating secret: ' + key);
|
||||
currentObj[key] = randomBytes(48).toString('base64').replace(/\W/g, '');
|
||||
}
|
||||
|
||||
@ -93,7 +100,6 @@ export default class config {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
**** Example ****
|
||||
|
Reference in New Issue
Block a user