Skip to content

Commit

Permalink
Merge pull request #107 from motdotla/replace-ignore-process-env
Browse files Browse the repository at this point in the history
replace ignoreProcessEnv with processEnv. inject your own
  • Loading branch information
motdotla committed Feb 9, 2024
2 parents dd80ec7 + 686bcd9 commit d9a452c
Show file tree
Hide file tree
Showing 11 changed files with 12,372 additions and 6,421 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github: motdotla
custom: https://www.dotenvx.com
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ node_modules

.DS_Store
.idea
.nyc_output
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

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
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
</div>
</a>
<br>
<a href="https://retool.com/?utm_source=sponsor&utm_campaign=dotenv">
<a href="https://workos.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=dotenv&utm_source=github">
<div>
<img src="https://res.cloudinary.com/dotenv-org/image/upload/c_scale,w_300/v1664466968/logo-full-black_vidfqf.png" width="270" alt="Retool">
<img src="https://res.cloudinary.com/dotenv-org/image/upload/c_scale,w_400/v1665605496/68747470733a2f2f73696e647265736f726875732e636f6d2f6173736574732f7468616e6b732f776f726b6f732d6c6f676f2d77686974652d62672e737667_zdmsbu.svg" width="270" alt="WorkOS">
</div>
<b>Retool helps developers build custom internal software, like CRUD apps and admin panels, really fast.</b>
<b>Your App, Enterprise Ready.</b>
<div>
<sup>Build UIs visually with flexible components, connect to any data source, and write business logic in JavaScript.</sup>
<sup>Add Single Sign-On, Multi-Factor Auth, and more, in minutes instead of months.</sup>
</div>
</a>
<br/>
Expand Down 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
Loading

0 comments on commit d9a452c

Please sign in to comment.