Created
July 25, 2016 15:10
-
-
Save guemidiborhane/b25ca1d577c87eb1cf006269eda2c2ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let obj = {} | |
let file = require('fs').readFileSync(__dirname + '/../../../../.js.env', 'utf8') | |
file.toString().split('\n').forEach(function (line) { | |
// matching "KEY' and 'VAL' in 'KEY=VAL' | |
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/) | |
// matched? | |
if (keyValueArr != null) { | |
var key = keyValueArr[1] | |
// default undefined or missing values to empty string | |
var value = keyValueArr[2] ? keyValueArr[2] : '' | |
// expand newlines in quoted values | |
var len = value ? value.length : 0 | |
if (len > 0 && value.charAt(0) === '\"' && value.charAt(len - 1) === '\"') { | |
value = value.replace(/\\n/gm, '\n') | |
} | |
// remove any surrounding quotes and extra spaces | |
value = value.replace(/(^['"]|['"]$)/g, '').trim() | |
obj[key] = value | |
} | |
}) | |
export default obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment