Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support variable expansion in the middle of a string. #97

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,34 @@ module.exports = {
value = value.replace(/\\n/gm, '\n')
}

var isSingleQuoted = value.charAt(0) === '\'' && value.charAt(len - 1) === '\''

// remove any surrounding quotes and extra spaces
value = value.replace(/(^['"]|['"]$)/g, '').trim()

// is this value a variable?
if (value.charAt(0) === '$') {
var possibleVar = value.substring(1)
value = obj[possibleVar] || process.env[possibleVar] || ''
}
// varaible can be escaped with a \$
if (value.substring(0, 2) === '\\$') {
value = value.substring(1)
// match and replace variables
if (!isSingleQuoted) {
value = value.replace(/\\?\${?[^}]*}?/g, function (match) {
var possibleVar
var len = match.length

// variable can be escaped with a \$
if (match.charAt(0) === '\\') {
return match.substring(1)
}

if (match.charAt(1) === '{') {
// if there is an opening brace there must be a closing one
if (match.charAt(len - 1) !== '}') {
return match
}
possibleVar = match.slice(2, -1)
} else {
possibleVar = match.substring(1)
}

return obj[possibleVar] || process.env[possibleVar] || ''
})
}

obj[key] = value
Expand Down
5 changes: 5 additions & 0 deletions test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ EQUAL_SIGNS=equals==
RETAIN_INNER_QUOTES={"foo": "bar"}
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'
INCLUDE_SPACE=some spaced out string
BRACED_EXPAND=${BASIC}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't look to be used in the new tests

INLINE_EXPAND="inline${BASIC}ally"
ESCAPED_INLINE_EXPAND="inline\${BASIC}ally"
SINGLE_QUOTED_INLINE_EXPAND='inline${BASIC}ally'
UNCLOSED_INLINE_EXPAND="inline${BASICally"
25 changes: 25 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ describe('dotenv', function () {
parsed.ESCAPED_EXPAND.should.equal('$ESCAPED')
done()
})

it('expands variables surrounded by braces', function (done) {
parsed.SINGLE_QUOTED_INLINE_EXPAND.should.equal('inline${BASIC}ally')
done()
})

it('expands inline environment variables', function (done) {
parsed.INLINE_EXPAND.should.equal('inlinebasically')
done()
})

it('does not expands inline escaped variables', function (done) {
parsed.ESCAPED_INLINE_EXPAND.should.equal('inline${BASIC}ally')
done()
})

it('does not expands inline variables in single quoted strings', function (done) {
parsed.SINGLE_QUOTED_INLINE_EXPAND.should.equal('inline${BASIC}ally')
done()
})

it('does not expands inline variables with unclosed braces', function (done) {
parsed.UNCLOSED_INLINE_EXPAND.should.equal('inline${BASICally')
done()
})
})

it('defaults empty values to empty string', function (done) {
Expand Down