Skip to content

Commit

Permalink
replace ignoreProcessEnv with processEnv. inject your own
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Feb 8, 2024
1 parent 6522c27 commit 4438bf8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ All notable changes to this project will be documented in this file. See [standa

## [11.0.0](https://github.com/motdotla/dotenv-expand/compare/v10.0.0...v11.0.0) (2024-02-08)

### Added

- Add typings for `import dotenv-expand/config` ([#99](https://github.com/motdotla/dotenv-expand/pull/99))
- Support expansion of dot in env variable names like `POSTGRESQL.BASE.USER` ([#93](https://github.com/motdotla/dotenv-expand/pull/93))
- Add `processEnv` option ([#105](https://github.com/motdotla/dotenv-expand/pull/105))

### Changed

- Do not expand prior `process.env` environment variables. NOTE: make sure to see updated README regarding `dotenv.config({ processEnv: {} })` ([#104](https://github.com/motdotla/dotenv-expand/pull/104))

### Removed

- Remove `ignoreProcessEnv` option (use `processEnv` option going forward)

## [10.0.0](https://github.com/motdotla/dotenv-expand/compare/v9.0.0...v10.0.0) (2022-12-16)

### Added
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,22 +155,24 @@ console.log(obj)

#### Options

##### ignoreProcessEnv
##### processEnv

Default: `false`
Default: `process.env`

Turn off writing to `process.env`.
Specify an object to write your secrets to. Defaults to `process.env` environment variables.

```js
const myObject = {}
const dotenv = {
ignoreProcessEnv: true,
processEnv: myObject,
parsed: {
SHOULD_NOT_EXIST: 'testing'
}
}
const obj = dotenvExpand.expand(dotenv).parsed

console.log(obj.SHOULD_NOT_EXIST) // testing
console.log(myObject.SHOULD_NOT_EXIST) // testing
console.log(process.env.SHOULD_NOT_EXIST) // undefined
```

Expand Down
24 changes: 13 additions & 11 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function _interpolate (value, processEnv, parsed) {
const replacementString = processEnv[key] || defaultValue || parsed[key] || ''
const modifiedValue = value.replace(group, replacementString)

// return early for scenario like process.env.PASSWORD = 'pas$word'
// return early for scenario like processEnv.PASSWORD = 'pas$word'
if (processEnv[key] && modifiedValue === processEnv[key]) {
return modifiedValue
}
Expand All @@ -52,28 +52,30 @@ function _resolveEscapeSequences (value) {
return value.replace(/\\\$/g, '$')
}

function expand (config) {
// if ignoring process.env, use a blank object
const processEnv = config.ignoreProcessEnv ? {} : process.env
function expand (options) {
let processEnv = process.env
if (options && options.processEnv != null) {
processEnv = options.processEnv
}

for (const key in config.parsed) {
let value = config.parsed[key]
for (const key in options.parsed) {
let value = options.parsed[key]

// don't interpolate the processEnv value if it exists there already
if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
value = processEnv[key]
} else {
value = _interpolate(value, processEnv, config.parsed)
value = _interpolate(value, processEnv, options.parsed)
}

config.parsed[key] = _resolveEscapeSequences(value)
options.parsed[key] = _resolveEscapeSequences(value)
}

for (const processKey in config.parsed) {
processEnv[processKey] = config.parsed[processKey]
for (const processKey in options.parsed) {
processEnv[processKey] = options.parsed[processKey]
}

return config
return options
}

module.exports.expand = expand

0 comments on commit 4438bf8

Please sign in to comment.