<commit_before>"use strict";

/*
Complete example with: multiple environments (production, development), cli
options and multiple config files. This is the most common scenario.

The "argp" module is used to parse the cli options.
*/

//Fake environment vars
process.env.NODE_ENV = "development";
process.env.DATABASE_HOSTNAME = "10.0.0.1";
process.env.DATABASE_PORT = "1337";

//Fake argv
process.argv = ["node", __filename, "--db-port", "1338"];

var seraphim = require ("../../lib");

var loadConfig = function (cb){
	//Configure CLI options
	var argv = require ("argp")
			.body ()
					.option ({
						long: "db-hostname",
						metavar: "HOST"
					})
					.option ({
						long: "db-port",
						metavar: "PORT",
						type: Number
					})
					.help ()
			.argv ();
	
	var envConfigFile;
	if (process.env.NODE_ENV === "production"){
		envConfigFile = "production.json";
	}else{
		envConfigFile = "development.json";
	}

	seraphim.createVault ()
			.on ("error", cb)
			.on ("end", function (){
				cb (null, this.get ());
			})
			//Common config file
			.load ("config1.json")
			//Common config file
			.load ("config2.json")
			//Environment specific file
			.load (envConfigFile)
			//Environment vars
			.load (function (){
				var o = this.get ();
				if (process.env.DATABASE_HOSTNAME){
					o.database.hostname = process.env.DATABASE_HOSTNAME;
				}
				if (process.env.DATABASE_PORT){
					o.database.port = parseInt (process.env.DATABASE_PORT);
				}
			})
			//CLI options
			.load (function (){
				var o = this.get ();
				if (argv["db-hostname"]) o.database.hostname = argv["db-hostname"];
				if (argv["db-port"]) o.database.port = argv["db-port"];
			})
};

loadConfig (function (error, config){
	if (error) return console.error (error);
	
	console.log (config);
	
	/*
	{
		log: {
			type: "circular",
			backup: "1week"
		},
		database: {
			hostname: "10.0.0.1",
			port: 1338
		},
		web: {
			hostname: "4.3.2.1",
			port: 4321<commit_msg>updated complete example<commit_after>"use strict";

/*
Complete example with: multiple environments (production, development), cli
options and multiple config files. This is the most common scenario.

The "argp" module is used to parse the cli options.
*/

//Fake environment vars
process.env.NODE_ENV = "development";
process.env.DATABASE_HOSTNAME = "10.0.0.1";
process.env.DATABASE_PORT = "1337";

//Fake argv
process.argv = ["node", __filename, "--db-port", "1338"];

//Configure CLI options
//The "argp" module prints a nice help message. Modify the previous fake argv
//and add the "-h" option to see it in action
var argv = require ("argp")
		.body ()
				.option ({
					long: "db-hostname",
					metavar: "HOST"
				})
				.option ({
					long: "db-port",
					metavar: "PORT",
					type: Number
				})
				.help ()
		.argv ();

var seraphim = require ("../../lib");

var loadConfig = function (argv, cb){
	var envConfigFile;
	if (process.env.NODE_ENV === "production"){
		envConfigFile = "production.json";
	}else{
		envConfigFile = "development.json";
	}

	seraphim.createVault ()
			.on ("error", cb)
			.on ("end", function (){
				cb (null, this.get ());
			})
			//Common config file
			.load ("config1.json")
			//Common config file
			.load ("config2.json")
			//Environment specific file
			.load (envConfigFile)
			//Environment vars
			.load (function (){
				var o = this.get ();
				if (process.env.DATABASE_HOSTNAME){
					o.database.hostname = process.env.DATABASE_HOSTNAME;
				}
				if (process.env.DATABASE_PORT){
					o.database.port = parseInt (process.env.DATABASE_PORT);
				}
			})
			//CLI options
			.load (function (){
				var o = this.get ();
				if (argv["db-hostname"]) o.database.hostname = argv["db-hostname"];
				if (argv["db-port"]) o.database.port = argv["db-port"];
			})
};

loadConfig (argv, function (error, config){
	if (error) return console.error (error);
	
	argv = null;
	console.log (config);
	
	/*
	{
		log: {
			type: "circular",
			backup: "1week"
		},
		database: {
			hostname: "10.0.0.1",
			port: 1338
		},
		web: {
			hostname: "4.3.2.1",
			port: 4321
{'fraction_non_alphanumeric': 0.16666666666666666, 'fraction_numerical': 0.016176470588235296, 'mean_word_length': 2.8682464454976304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '7628536', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1415, 'n_words': 366}