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

Add default expansion. #39

Merged
merged 6 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ var dotenvExpand = function (config) {
var environment = config.ignoreProcessEnv ? {} : process.env

var interpolate = function (envValue) {
var matches = envValue.match(/(.?\${?(?:[a-zA-Z0-9_]+)?}?)/g) || []
var matches = envValue.match(/(.?\${*[\w]*(?::-)?[\w]*}*)/g) || []

return matches.reduce(function (newEnv, match, index) {
var parts = /(.?)\${*([\w]*(?::-)?[\w]*)?}*/g.exec(match)
if(!parts || parts.length === 0) {
return newEnv;
}

return matches.reduce(function (newEnv, match) {
var parts = /(.?)\${?([a-zA-Z0-9_]+)?}?/g.exec(match)
var prefix = parts[1]

var value, replacePart
Expand All @@ -17,11 +21,21 @@ var dotenvExpand = function (config) {
replacePart = parts[0]
value = replacePart.replace('\\$', '$')
} else {
var key = parts[2]
var keyParts = parts[2].split(':-')
var key = keyParts[0]
replacePart = parts[0].substring(prefix.length)
// process.env value 'wins' over .env file's value
value = environment.hasOwnProperty(key) ? environment[key] : (config.parsed[key] || '')
value = environment.hasOwnProperty(key)
? environment[key]
: (config.parsed[key] || keyParts[1] || '')

// If the value is found, remove nested expansions.
if(keyParts.length > 1 && value) {
var replaceNested = matches[index+1]
matches[index + 1] = ''

newEnv = newEnv.replace(replaceNested, '')
}
// Resolve recursive interpolations
value = interpolate(value)
}
Expand Down
6 changes: 6 additions & 0 deletions test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ MACHINE=machine_env
MACHINE_EXPAND=$MACHINE
UNDEFINED_EXPAND=$UNDEFINED_ENV_KEY
ESCAPED_EXPAND=\$ESCAPED
DEFINED_EXPAND_WITH_DEFAULT=${MACHINE:-default}
DEFINED_EXPAND_WITH_DEFAULT_NESTED=${MACHINE:-${UNDEFINED_ENV_KEY:-default}}
UNDEFINED_EXPAND_WITH_DEFINED_NESTED=${UNDEFINED_ENV_KEY:-${MACHINE:-default}}
UNDEFINED_EXPAND_WITH_DEFAULT=${UNDEFINED_ENV_KEY:-default}
UNDEFINED_EXPAND_WITH_DEFAULT_NESTED=${UNDEFINED_ENV_KEY:-${UNDEFINED_ENV_KEY_2:-default}}
UNDEFINED_EXPAND_WITH_DEFAULT_NESTED_TWICE=${UNDEFINED_ENV_KEY:-${UNDEFINED_ENV_KEY_2${UNDEFINED_ENV_KEY_3:-default}}}
Copy link
Contributor

@FezVrasta FezVrasta Mar 30, 2022

Choose a reason for hiding this comment

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

@vantreeseba may you help me understand how is this supposed to work? Why are UNDEFINED_ENV_KEY2 and ${UNDEFINED_ENV_KEY_3:-default} being concatenated?

Should ${MACHINE${MACHINE}} resolve to machinemachine?

This syntax doesn't seem to work on Bash, I get bad substitution. The correct one should be this I think:

UNDEFINED_EXPAND_WITH_DEFAULT_NESTED_TWICE=${UNDEFINED_ENV_KEY:-${UNDEFINED_ENV_KEY_2}${UNDEFINED_ENV_KEY_3:-default}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That actually looks like my test was missing a :-

So it should look like

UNDEFINED_EXPAND_WITH_DEFAULT_NESTED_TWICE=${UNDEFINED_ENV_KEY:-${UNDEFINED_ENV_KEY_2:-${UNDEFINED_ENV_KEY_3:-default}}}

Idea being, it's a n, n+1,n+2 test to do a "proof by induction".

I can fix it, or if you want to fix it in your branch is fine, whichever everyone thinks will be easier/better.

It passed due to the empty env vars being resolved to empty strings, so it probably should have more test cases like the previous defined/nested undefined ones.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in my branch, thanks for confirming!

MONGOLAB_DATABASE=heroku_db
MONGOLAB_USER=username
MONGOLAB_PASSWORD=password
Expand Down
46 changes: 46 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,52 @@ describe('dotenv-expand', function () {
done()
})

it('expands environment variables existing already on the machine even with a default', function (done) {
process.env.MACHINE = 'machine'
dotenvExpand(dotenv)

process.env['DEFINED_EXPAND_WITH_DEFAULT'].should.eql('machine')
done()
})

it('expands environment variables existing already on the machine even with a default when nested', function (done) {
process.env.MACHINE = 'machine'
dotenvExpand(dotenv)

process.env['DEFINED_EXPAND_WITH_DEFAULT_NESTED'].should.eql('machine')
done()
})

it('expands environment variables undefined with one already on the machine even with a default when nested', function (done) {
process.env.MACHINE = 'machine'
dotenvExpand(dotenv)

process.env['UNDEFINED_EXPAND_WITH_DEFINED_NESTED'].should.eql('machine')
done()
})

it('expands missing environment variables to an empty string but replaces with default', function(done){
var obj = dotenvExpand(dotenv).parsed

obj['UNDEFINED_EXPAND_WITH_DEFAULT'].should.eql('default')
done()
})

it('expands missing environment variables to an empty string but replaces with default nested', function(done){
var obj = dotenvExpand(dotenv).parsed

obj['UNDEFINED_EXPAND_WITH_DEFAULT_NESTED'].should.eql('default')
done()
})

it('expands missing environment variables to an empty string but replaces with default nested twice', function(done){
var obj = dotenvExpand(dotenv).parsed

obj['UNDEFINED_EXPAND_WITH_DEFAULT_NESTED_TWICE'].should.eql('default')
done()
})


it('prioritizes machine key expansion over .env', function (done) {
process.env.MACHINE = 'machine'
var obj = dotenvExpand(dotenv).parsed
Expand Down