Skip to content

Commit

Permalink
Merge d622e10 into 472db2e
Browse files Browse the repository at this point in the history
  • Loading branch information
ovictoraurelio committed Feb 7, 2018
2 parents 472db2e + d622e10 commit d71c914
Show file tree
Hide file tree
Showing 3 changed files with 4,427 additions and 1 deletion.
29 changes: 28 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ const path = require('path')
*/
function parse (src) {
const obj = {}
const envLines = src.toString().split('\n')

// convert Buffers before splitting into lines and processing
src.toString().split('\n').forEach(function (line) {
envLines.forEach(function (line, index) {
// matching "KEY' and 'VAL' in 'KEY=VAL'
const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
// matched?
Expand All @@ -31,6 +32,32 @@ function parse (src) {
// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

// get any object in pattern: myObject={
// # comment example
// myProp: "This example with } inside string works fine"
// }
if (len > 0 && value.charAt(0) === '{') {
let objStr = value
let closedBrackets = -1

for (let i = index + 1; i < envLines.length; i++) {
if (!(envLines[i].indexOf('#') > -1 && envLines[i].match(/(?!\B"[^"]*)#(?![^"]*"\B)/g))) {
objStr += envLines[i]

if (envLines[i].indexOf('}') > -1 && envLines[i].match(/(?!\B"[^"]*)}(?![^"]*"\B)/g)) {
closedBrackets++
} else if (envLines[i].indexOf('{') > -1 && envLines[i].match(/(?!\B"[^"]*){(?![^"]*"\B)/g)) {
closedBrackets--
}

if (closedBrackets === 0) {
value = JSON.parse(objStr)
break
}
}
}
}

obj[key] = value
}
})
Expand Down
Loading

0 comments on commit d71c914

Please sign in to comment.