'use strict';

const _ = require('lodash');
const envfile = require('envfile');

module.exports = {
  /**
   * Parses a .env file and returns it as JSON.
   * @param  {string} file
   * @return {Promise}
   */
  parse(file) {
    return new Promise((resolve, reject) => {
      const configAsJson = envfile.parseFileSync(file);

      if (configAsJson) {
        resolve(configAsJson);
      } else {
        reject('Config file not found.');
      }
    });
  },

  /**
   * Compiles the config.
   * @param  {Object} config
   * @param  {string} file
   * @return {Promise}
   */
  compile(config, file) {
    return new Promise((resolve) => {
      file = file || '.env';

      let configAsString = '';

      _.forEach(config, (choice, name) => {
        if (choice) {
          configAsString += `${name}=${choice}\n`;
        }
      });

      return resolve(configAsString);
    });
  }
};

@@ -285,20 +285,19 @@
 configAs
-Json
+Env
  = envfi
@@ -319,16 +319,162 @@
 c(file);
+%0A      let configAsJson = %7B%7D;%0A%0A      _.forEach(configAsEnv, (value, propertyPath) =%3E %7B%0A        _.set(configAsJson, propertyPath, value);%0A      %7D);
 %0A%0A      

Add nested support for .env files