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

Optional appropriate environ replace #93

Closed
wants to merge 1 commit into from
Closed
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
20 changes: 16 additions & 4 deletions lib/config/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,23 @@ function envReplace (f) {
return f.replace(envExpr, function (orig, esc, name) {
esc = esc.length && esc.length % 2
if (esc) return orig
if (undefined === process.env[name]) {
throw new Error('Failed to replace env in config: ' + orig)
var failbackExpr = /(\\*)-/g
var failbackMatch
var failback
while ((failbackMatch = failbackExpr.exec(name))) {
esc = failbackMatch[1]
esc = esc.length && esc.length % 2
if (esc) continue
failback = name.slice(failbackMatch.index + 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you have an even number of \ chars, then this will be the wrong offset. https://gist.github.com/isaacs/c2b12db30a49324325a3781302668408

I recommend writing the failback parsing logic as a new npm module, so that it can get proper tests and documentation.

name = name.slice(0, failbackMatch.index)
break
}

return process.env[name]
if (undefined !== process.env[name]) {
return process.env[name]
} else if (failbackMatch) {
return failback
}
throw new Error('Failed to replace env in config: ' + orig)
})
}

Expand Down