<commit_before>/*
 * Author: <NAME> (Oxyno-zeta)
 * Date: 03/07/16
 * Licence: See Readme
 */

/* ************************************* */
/* ********       REQUIRE       ******** */
/* ************************************* */
const nconf = require('nconf');
const _ = require('lodash');
const path = require('path');

// Variables
const CONSTANTS = {
	PORT: 'CRASH_REPORTER_PORT',
	LOG_LEVEL: 'CRASH_REPORTER_LOG_LEVEL',
	LOG_UPLOAD_DIR: 'CRASH_REPORTER_LOG_UPLOAD_DIR',
	LOG_APP_CRASH_DIR: 'CRASH_REPORTER_LOG_APP_CRASH_DIR',
	DATABASE_URL: 'CRASH_REPORTER_DATABASE_URL',
	DATABASE_LOGIN: 'CRASH_REPORTER_DATABASE_LOGIN',
	DATABASE_PASSWORD: '<PASSWORD>',
	GITHUB_OAUTH_ENABLED: 'CRASH_REPORTER_AUTH_GITHUB_OAUTH_ENABLED',
	GITHUB_CLIENT_ID: 'CRASH_REPORTER_AUTH_GITHUB_CLIENT_ID',
	GITHUB_CLIENT_SECRET: 'CRASH_REPORTER_AUTH_GITHUB_CLIENT_SECRET',
	JWT_SECRET: 'CRASH_REPORTER_AUTH_JWT_SECRET'
};
const LOG_LEVELS = {
	DEBUG: 'debug',
	INFO: 'info',
	ERROR: 'error',
	WARN: 'warn'
};

// Default configuration
const DEFAULT_CONFIG = {
	'CRASH_REPORTER_PORT': 2000,
	'CRASH_REPORTER_LOG_LEVEL': LOG_LEVELS.INFO,
	'CRASH_REPORTER_LOG_UPLOAD_DIR': path.join(process.cwd(), 'upload-logs/'),
	'CRASH_REPORTER_LOG_APP_CRASH_DIR': path.join(process.cwd(), 'app-crash-logs/'),
	'CRASH_REPORTER_DATABASE_URL': '',
	'CRASH_REPORTER_DATABASE_LOGIN': '',
	'CRASH_REPORTER_DATABASE_PASSWORD': '',
	'CRASH_REPORTER_AUTH_GITHUB_OAUTH_ENABLED': true,
	'CRASH_REPORTER_AUTH_GITHUB_CLIENT_ID': '',
	'CRASH_REPORTER_AUTH_GITHUB_CLIENT_SECRET': '',
	'CRASH_REPORTER_AUTH_JWT_SECRET': ''
};

/**
 * 1) Default
 * 2) Configuration file
 * 3) Env variables
 * 4) Command line
 */
// Configuration file => no config file for the moment
// nconf.file({file: 'config.json'});
// Environment variables
nconf.env();
// Command line
nconf.argv();
// Default
nconf.defaults(DEFAULT_CONFIG);

/* ************************************* */
/* ********        EXPORTS      ******** */
/* ************************************* */

module.exports = {
	CONSTANTS: CONSTANTS,
	getConfig: getConfig
};

/* ************************************* */
/* ********  PRIVATE FUNCTIONS  ******** */
/* ************************************* */



/* ************************************* */
/* ********   PUBLIC FUNCTIONS  ******** */
/* ************************************* */

/**
 * Get configuration.
 * @returns {{}}
 */
function getConfig() {
	var config = {};

	var key;
	for (key in DEFAULT_CONFIG) {
		if (DEFAULT_CONFIG.hasOwnProperty(key)) {
			config[key] = nconf.get(key);
		}
	}

	// Valid log level
	if (!_.includes(LOG_LEVELS, config[CONSTANTS.LOG_LEVEL])) {
		config[CONSTANTS.LOG_LEVEL] = LOG_LEVELS.INFO;
	}

	// Change type if necessary
	if (!_.isInteger(config[CONSTANTS.PORT])) {
		config[CONSTANTS.PORT] = _.parseInt(config[CONSTANTS.PORT]);
	}
	if (!_.isBoolean(config[CONSTANTS.GITHUB_OAUTH_ENABLED])) {
		config[CONSTANTS.GITHUB_OAUTH_ENABLED] = (config[CONSTANTS.GITHUB_OAUTH_ENABLED === 'true']);
	}

	return config;
}<commit_msg>[Configuration] Fix configuration wrapper<commit_after>/*
 * Author: <NAME> (Oxyno-zeta)
 * Date: 03/07/16
 * Licence: See Readme
 */

/* ************************************* */
/* ********       REQUIRE       ******** */
/* ************************************* */
const nconf = require('nconf');
const _ = require('lodash');
const path = require('path');

// Variables
const CONSTANTS = {
	PORT: 'CRASH_REPORTER_PORT',
	LOG_LEVEL: 'CRASH_REPORTER_LOG_LEVEL',
	LOG_UPLOAD_DIR: 'CRASH_REPORTER_LOG_UPLOAD_DIR',
	LOG_APP_CRASH_DIR: 'CRASH_REPORTER_LOG_APP_CRASH_DIR',
	DATABASE_URL: 'CRASH_REPORTER_DATABASE_URL',
	DATABASE_LOGIN: 'CRASH_REPORTER_DATABASE_LOGIN',
	DATABASE_PASSWORD: '<PASSWORD>',
	GITHUB_OAUTH_ENABLED: 'CRASH_REPORTER_AUTH_GITHUB_OAUTH_ENABLED',
	GITHUB_CLIENT_ID: 'CRASH_REPORTER_AUTH_GITHUB_CLIENT_ID',
	GITHUB_CLIENT_SECRET: 'CRASH_REPORTER_AUTH_GITHUB_CLIENT_SECRET',
	JWT_SECRET: 'CRASH_REPORTER_AUTH_JWT_SECRET'
};
const LOG_LEVELS = {
	DEBUG: 'debug',
	INFO: 'info',
	ERROR: 'error',
	WARN: 'warn'
};

// Default configuration
const DEFAULT_CONFIG = {
	'CRASH_REPORTER_PORT': 2000,
	'CRASH_REPORTER_LOG_LEVEL': LOG_LEVELS.INFO,
	'CRASH_REPORTER_LOG_UPLOAD_DIR': path.join(process.cwd(), 'upload-logs/'),
	'CRASH_REPORTER_LOG_APP_CRASH_DIR': path.join(process.cwd(), 'app-crash-logs/'),
	'CRASH_REPORTER_DATABASE_URL': '',
	'CRASH_REPORTER_DATABASE_LOGIN': '',
	'CRASH_REPORTER_DATABASE_PASSWORD': '',
	'CRASH_REPORTER_AUTH_GITHUB_OAUTH_ENABLED': true,
	'CRASH_REPORTER_AUTH_GITHUB_CLIENT_ID': '',
	'CRASH_REPORTER_AUTH_GITHUB_CLIENT_SECRET': '',
	'CRASH_REPORTER_AUTH_JWT_SECRET': ''
};

/**
 * 1) Default
 * 2) Configuration file
 * 3) Env variables
 * 4) Command line
 */
// Configuration file => no config file for the moment
// nconf.file({file: 'config.json'});
// Environment variables
nconf.env();
// Command line
nconf.argv();
// Default
nconf.defaults(DEFAULT_CONFIG);

/* ************************************* */
/* ********        EXPORTS      ******** */
/* ************************************* */

module.exports = {
	CONSTANTS: CONSTANTS,
	getConfig: getConfig
};

/* ************************************* */
/* ********  PRIVATE FUNCTIONS  ******** */
/* ************************************* */



/* ************************************* */
/* ********   PUBLIC FUNCTIONS  ******** */
/* ************************************* */

/**
 * Get configuration.
 * @returns {{}}
 */
function getConfig() {
	var config = {};

	var key;
	for (key in DEFAULT_CONFIG) {
		if (DEFAULT_CONFIG.hasOwnProperty(key)) {
			config[key] = nconf.get(key);
		}
	}

	// Valid log level
	if (!_.includes(LOG_LEVELS, config[CONSTANTS.LOG_LEVEL])) {
		config[CONSTANTS.LOG_LEVEL] = LOG_LEVELS.INFO;
	}

	// Change type if necessary
	if (!_.isInteger(config[CONSTANTS.PORT])) {
		config[CONSTANTS.PORT] = _.parseInt(config[CONSTANTS.PORT]);
	}
	if (!_.isBoolean(config[CONSTANTS.GITHUB_OAUTH_ENABLED])) {
		config[CONSTANTS.GITHUB_OAUTH_ENABLED] = (config[CONSTANTS.GITHUB_OAUTH_ENABLED] === 'true');
	}

	return config;
}
{'fraction_non_alphanumeric': 0.2537165510406343, 'fraction_numerical': 0.004625041295011563, 'mean_word_length': 5.668502202643172, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '912194', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2258, 'n_words': 311}