{"size": 1934, "ext": "js", "max_stars_repo_path": "appevent.js", "max_stars_repo_name": "evaisse/node-appevent", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "appevent.js", "max_issues_repo_name": "evaisse/node-appevent", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "appevent.js", "max_forks_repo_name": "evaisse/node-appevent", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.9381443299, "max_line_length": 84, "alphanum_fraction": 0.5486039297}
/**
 * @author evaisse
 */
var restoreConsole = require('jsonconsole')();
var dotenv = require('dotenv').config({ silent: true });
var nconf = require('nconf');
var os = require('os');
var util = require('util');


var defaultConfig = {

    "NODE_ENV": {
        alias: "env",
        description: 'Global node env',
        default: "production"
    },

    "OUTPUT_VERBOSE": {
        alias: "verbose",
        description: 'Enable debug level output',
        default: false,
        type: "boolean",
    },

    "h": {
        description: "Show this help usage",
        type: "boolean",
    },

    "V": {
        description: "Show current version",
        type: "boolean"
    },

};




/*
    Build environment regarding
        1) process.env
        2) dotenv() overrdie regarding .env file on root dir
        3) argv values
 */
var buildEnvironmentConfig = function () {

    /*
        Assign env keys to config only assign 
        config that match a CONSTANT_FORM_PATTERN LIKE_THAT
     */
    nconf.env(Object.keys(process.args))
         .argv(process.args);

    /*
        Show help if needed
     */
    if (nconf.get('h')) {
        process.stdout.write(nconf.stores.argv.help());
        process.exit();
    }

    /*
        Stop command for some args
     */
    if (nconf.get('V')) {
        process.stdout.write(require(__dirname + '/package.json').version + os.EOL);
        process.exit();
    }

    /*
        rewrite only env keys
     */
    Object.keys(nconf.get())
          .filter((k) => k.match(/[A-Z0-9][A-Z0-9_]+[A-Z0-9]/))
          .forEach((key) => process.env[key] = nconf.get(key));

}


/**
 * Exports a start() method
 */
module.exports = function () {

    process.args = process.args || defaultConfig;
    process.once('ready', buildEnvironmentConfig);

    return function () {
        process.emit('load');
        process.emit('ready');
        process.emit('start');
    };

};
