Skip to content
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
17 changes: 13 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ function _resolveEscapeSequences (value) {
return value.replace(/\\\$/g, '$')
}

function expandValue (value, processEnv, runningParsed) {
const env = { ...runningParsed, ...processEnv } // process.env wins
const propertyIsEnumerable = Object.prototype.propertyIsEnumerable

function getEnvValue (key, processEnv, runningParsed) {
if (propertyIsEnumerable.call(processEnv, key)) {
return processEnv[key] // process.env wins
}

return runningParsed[key]
}

function expandValue (value, processEnv, runningParsed) {
const regex = /(?<!\\)\${([^{}]+)}|(?<!\\)\$([A-Za-z_][A-Za-z0-9_]*)/g

let result = value
Expand All @@ -31,13 +39,14 @@ function expandValue (value, processEnv, runningParsed) {
let value

const key = r.shift()
const envValue = getEnvValue(key, processEnv, runningParsed)

if ([':+', '+'].includes(splitter)) {
defaultValue = env[key] ? r.join(splitter) : ''
defaultValue = envValue ? r.join(splitter) : ''
value = null
} else {
defaultValue = r.join(splitter)
value = env[key]
value = envValue
}

if (value) {
Expand Down
21 changes: 21 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,27 @@ t.test('can write to an object rather than process.env if user provides it', ct
ct.end()
})

t.test('only expands enumerable processEnv properties', ct => {
const processEnv = {}
Object.defineProperty(processEnv, 'SECRET', {
enumerable: false,
writable: true,
value: 'secret'
})

const dotenv = {
processEnv,
parsed: {
SECRET_EXPAND: '$SECRET'
}
}
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.SECRET_EXPAND, '')

ct.end()
})

t.test('expands environment variables existing already on the machine even with a default with special characters', ct => {
const dotenv = require('dotenv').config({ path: 'tests/.env.test', processEnv: {} })
const parsed = dotenvExpand.expand(dotenv).parsed
Expand Down