
var fs = require('fs');
var type = Function.prototype.call.bind(Object.prototype.toString);

function getEnv(options) {
  return (options || {}).env || process.env.NODE_ENV || 'development';
}

function isPlainObject(obj) {
  return type(obj) === '[object Object]';
}

function getConfig(path, env, callback) {
  var stat = fs.statSync(path);
  var exists = stat.isFile();
  if (!exists) {
    return null;
  }

  var configFile = require(path);
  return configFile[env];
}

function upcase(word) {
  if (!word) { return word; }
  return word.toUpperCase();
}

function downcase(word) {
  if (!word) { return word; }
  return word.toLowerCase();
}

function pascalCaseToSnakeCase(word) {
  var matches = word.match(/^[a-z]+|[A-Z][a-z]+/g);
  return downcase(matches.join('_'));
}

function hyphenCaseToSnakeCase(word) {
  var matches = word.split(/\-+/g);
  return downcase(matches.join('_'));
}

function snakeCase(word) {
  return hyphenCaseToSnakeCase(pascalCaseToSnakeCase(word));
}

function flattenForEnv(config) {
  var envConfig = {};

  Object.keys(config).forEach(function (key) {
    var value = config[key];
    var envKey = upcase(snakeCase(key));

    if (isPlainObject(value)) {
      var innerEnvConfig = flattenForEnv(value);
      Object.keys(innerEnvConfig).forEach(function (innerKey) {
        var innerValue = innerEnvConfig[innerKey];
        var innerEnvKey = envKey + '_' + upcase(innerKey);
        envConfig[innerEnvKey] = innerValue;
      });
    } else {
      envConfig[envKey] = value;
    }
  });

  return envConfig;
}

function applyConfigToEnv(config) {
  var appliedConfig = {};
  Object.keys(config).forEach(function (key) {
    var value = config[key];
    var envValue = process.env[key];
    process.env[key] = appliedConfig[key] = envValue || value;
  });

  return appliedConfig;
}

module.exports = {
  load: function (path, options) {
    var env = getEnv(options);
    var config = getConfig(path, env);
    if (!config) {
      throw 'enfig: Could not find configuration for ' + env + ' at ' + path;
    }

    var envConfig = flattenForEnv(config);
    var appliedConfig = applyConfigToEnv(envConfig);
    return {config: config, env: envConfig, applied: appliedConfig};
  }
};

@@ -18,16 +18,44 @@
 ('fs');%0A
+var path = require('path');%0A
 var type
@@ -115,16 +115,17 @@
 ring);%0A%0A
+%0A
 function
@@ -129,100 +129,121 @@
 ion 
-getEnv(options) %7B%0A  return (options %7C%7C %7B%7D).env %7C%7C process.env.NODE_ENV %7C%7C 'development'
+makeArray(args) %7B%0A  var output = %5B%5D;%0A  for (var n in args) %7B%0A    output.push(args%5Bn%5D);%0A  %7D%0A%0A  return output
 ;%0A%7D%0A%0A
+%0A
 func
@@ -309,24 +309,599 @@
 bject%5D';%0A%7D%0A%0A
+%0Afunction parseOptions(args) %7B%0A  var argsArray = makeArray(args);%0A  var opts = getOptions(argsArray);%0A%0A  return %7B%0A    path: getPath(argsArray),%0A    env: opts.env %7C%7C process.env.NODE_ENV %7C%7C 'development'%0A  %7D;%0A%7D%0A%0A%0Afunction getOptions(args) %7B%0A  var output = %7B%7D;%0A  args.forEach(function (a) %7B%0A    if (isPlainObject(a)) %7B%0A      output = a;%0A      return;%0A    %7D%0A  %7D);%0A%0A  return output;%0A%7D%0A%0A%0Afunction getPath(args) %7B%0A  var segments = %5B%5D;%0A  args.forEach(function (a) %7B%0A    if (typeof a === 'string') %7B%0A      segments.push(a);%0A    %7D%0A  %7D);%0A%0A  return path.join.apply(path, segments);%0A%7D%0A%0A%0A
 function get
@@ -2449,95 +2449,68 @@
 %0A%7D%0A%0A
-module.exports = %7B%0A  load: function (path, options
+%0Afunction load(
 ) %7B%0A  
- 
- 
 var 
-env = getEnv(option
+options = parseOptions(argument
 s);%0A
-  
   va
@@ -2534,22 +2534,37 @@
 fig(
-path, 
+options.path, options.
 env);%0A
-  
+%0A
   if
@@ -2572,26 +2572,24 @@
 (!config) %7B%0A
-  
     throw 'e
@@ -2631,16 +2631,24 @@
 for ' + 
+options.
 env + ' 
@@ -2658,23 +2658,27 @@
 ' + 
+options.
 path;%0A
-  
   %7D%0A%0A
-  
   va
@@ -2714,18 +2714,16 @@
 onfig);%0A
-  
   var ap
@@ -2765,18 +2765,17 @@
 onfig);%0A
-  
+%0A
   return
@@ -2837,11 +2837,48 @@
 g%7D;%0A
-  %7D%0A%7D
+%7D%0A%0Aload.load = load;%0Amodule.exports = load
 ;%0A

add support for passing the config path as arguments to be joined