<commit_before>/// <reference path="../../../typings/globals/js-yaml/index.d.ts" />
/// <reference path="../../../typings/globals/node/index.d.ts" />

import Environment from '../chook/Environment';
import * as Crypto from 'crypto';
import * as Utils from '../chook/Utils';
import * as Yaml from 'js-yaml';

const Path = require( 'path' );
const Fs = require( 'fs' );

const configFileName: string = 'config.yml';

export default class Configuration{

	configurationPath: string;
	configuration: any;

	constructor(){
		let configurationFile: string = configFileName;
		if( process.env.NODE_ENV == 'mocha' )
			configurationFile = 'test.config.yml';
		this.configurationPath = Path.join( Environment.buildDirectory, 'resources', configurationFile );
		this.readConfiguration();
	}

	readConfiguration(): void{
		let fileContent: string = this.getFileContent( this.configurationPath );
		this.configuration = Yaml.safeLoad( fileContent );
	}

	getFileContent( filePath: string ): string{
		let fileContent: string;
		try{
			fileContent = Fs.readFileSync( filePath, 'utf8' );
		}catch( error ){
			this.generateDefaultConfiguration();
			fileContent = Fs.readFileSync( filePath, 'utf8' );
		}
		return fileContent;
	}

	dumpConfiguration( configurationObject: any ): void{
		Fs.writeFileSync( this.configurationPath, Yaml.safeDump( configurationObject ) );
	}

	generateDefaultConfiguration(): void{
		let defaultConfiguration: any = this.getDefaultConfiguration();
		this.dumpConfiguration( defaultConfiguration );
	}

	getDefaultConfiguration(): any{
		let jwtSecret: string = Crypto.randomBytes( 32 ).toString( 'hex' );
		let defaultConfiguration: any = {
			gui: false,
			security: {
				jwt: `${jwtSecret}`
			}
		};
		return defaultConfiguration;
	}

	get( ...keys: string[] ): any{
		return Utils.getNestedValue( this.configuration, ...keys );
	}

	set( value: any, ...keys: string[] ): void{
		this.configuration = Utils.setNestedValue( this.configuration, value, ...keys );
		this.dumpConfiguration( this.configuration );
	}

}<commit_msg>Configuration is now a singleton<commit_after>/// <reference path="../../../typings/globals/js-yaml/index.d.ts" />
/// <reference path="../../../typings/globals/node/index.d.ts" />

import Environment from '../chook/Environment';
import * as Crypto from 'crypto';
import * as Utils from '../chook/Utils';
import * as Yaml from 'js-yaml';

const Path = require( 'path' );
const Fs = require( 'fs' );

const configFileName: string = 'config.yml';

class Configuration{

	configurationPath: string;
	configuration: any;

	constructor(){
		let configurationFile: string = configFileName;
		if( process.env.NODE_ENV == 'mocha' )
			configurationFile = 'test.config.yml';
		this.configurationPath = Path.join( Environment.buildDirectory, 'resources', configurationFile );
		this.readConfiguration();
	}

	readConfiguration(): void{
		let fileContent: string = this.getFileContent( this.configurationPath );
		this.configuration = Yaml.safeLoad( fileContent );
	}

	getFileContent( filePath: string ): string{
		let fileContent: string;
		try{
			fileContent = Fs.readFileSync( filePath, 'utf8' );
		}catch( error ){
			this.generateDefaultConfiguration();
			fileContent = Fs.readFileSync( filePath, 'utf8' );
		}
		return fileContent;
	}

	dumpConfiguration( configurationObject: any ): void{
		Fs.writeFileSync( this.configurationPath, Yaml.safeDump( configurationObject ) );
	}

	generateDefaultConfiguration(): void{
		let defaultConfiguration: any = this.getDefaultConfiguration();
		this.dumpConfiguration( defaultConfiguration );
	}

	getDefaultConfiguration(): any{
		let jwtSecret: string = Crypto.randomBytes( 32 ).toString( 'hex' );
		let defaultConfiguration: any = {
			gui: false,
			server: {
				hostname: '0.0.0.0',
				port: 2555
			},
			security: {
				jwt: `${jwtSecret}`
			}
		};
		return defaultConfiguration;
	}

	get( ...keys: string[] ): any{
		return Utils.getNestedValue( this.configuration, ...keys );
	}

	set( value: any, ...keys: string[] ): void{
		this.configuration = Utils.setNestedValue( this.configuration, value, ...keys );
		this.dumpConfiguration( this.configuration );
	}

}

const configuration: Configuration = new Configuration();
export default configuration;
{'fraction_non_alphanumeric': 0.14548022598870056, 'fraction_numerical': 0.003766478342749529, 'mean_word_length': 4.226322263222632, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1444745', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1254, 'n_words': 309}