Skip to content

Commit

Permalink
Merge pull request #753 from motdotla/target
Browse files Browse the repository at this point in the history
Add ability to change the target from process.env to your own object
  • Loading branch information
motdotla committed Jun 16, 2023
2 parents 75cee88 + ea8db8c commit 406f2c3
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,10 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.1.0...master)
## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.2.0...master)

## [16.2.0](https://github.com/motdotla/dotenv/compare/v16.1.4...v16.2.0) (2023-06-15)

### Added

- Optionally write to your own target object rather than `process.env`. Defaults to `process.env`. [#753](https://github.com/motdotla/dotenv/pull/753)
- Add import type URL to types file [#751](https://github.com/motdotla/dotenv/pull/751)

## [16.1.4](https://github.com/motdotla/dotenv/compare/v16.1.3...v16.1.4) (2023-06-04)
Expand Down
44 changes: 37 additions & 7 deletions README.md
Expand Up @@ -283,7 +283,7 @@ You can additionally, pass options to `config`.

#### Options

##### Path
##### path

Default: `path.resolve(process.cwd(), '.env')`

Expand All @@ -293,7 +293,7 @@ Specify a custom path if your file containing environment variables is located e
require('dotenv').config({ path: '/custom/path/to/.env' })
```

##### Encoding
##### encoding

Default: `utf8`

Expand All @@ -303,7 +303,7 @@ Specify the encoding of your file containing environment variables.
require('dotenv').config({ encoding: 'latin1' })
```

##### Debug
##### debug

Default: `false`

Expand All @@ -313,7 +313,7 @@ Turn on logging to help debug why certain keys or values are not being set as yo
require('dotenv').config({ debug: process.env.DEBUG })
```

##### Override
##### override

Default: `false`

Expand All @@ -323,6 +323,20 @@ Override any environment variables that have already been set on your machine wi
require('dotenv').config({ override: true })
```

##### processEnv

Default: `process.env`

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

```js
const myObject = {}
require('dotenv').config({ processEnv: myObject })

console.log(myObject) // values from .env or .env.vault live here now.
console.log(process.env) // this was not changed or written to
```

### Parse

The engine which parses the contents of your file containing environment
Expand All @@ -338,7 +352,7 @@ console.log(typeof config, config) // object { BASIC : 'basic' }

#### Options

##### Debug
##### debug

Default: `false`

Expand Down Expand Up @@ -379,20 +393,36 @@ dotenv.populate(target, parsed, { override: true, debug: true })
console.log(target) // { HELLO: 'universe' }
```

#### Options
#### options

##### Debug

Default: `false`

Turn on logging to help debug why certain keys or values are not being populated as you expect.

##### Override
##### override

Default: `false`

Override any environment variables that have already been set.

### Decrypt

The engine which decrypts the ciphertext contents of your .env.vault file is available for use. It accepts a ciphertext and a decryption key. It uses AES-256-GCM encryption.

For example, decrypting a simple ciphertext:

```js
const dotenv = require('dotenv')
const ciphertext = 's7NYXa809k/bVSPwIAmJhPJmEGTtU0hG58hOZy7I0ix6y5HP8LsHBsZCYC/gw5DDFy5DgOcyd18R'
const decryptionKey = 'ddcaa26504cd70a6fef9801901c3981538563a1767c297cb8416e8a38c62fe00'

const decrypted = dotenv.decrypt(ciphertext, decryptionKey)

console.log(decrypted) // # development@v6\nALPHA="zeta"
```

## ❓ FAQ

### Why is the `.env` file not loading my environment variables successfully?
Expand Down
14 changes: 12 additions & 2 deletions lib/main.js
Expand Up @@ -162,7 +162,12 @@ function _configVault (options) {

const parsed = DotenvModule._parseVault(options)

DotenvModule.populate(process.env, parsed, options)
let processEnv = process.env
if (options && options.processEnv != null) {
processEnv = options.processEnv
}

DotenvModule.populate(processEnv, parsed, options)

return { parsed }
}
Expand All @@ -185,7 +190,12 @@ function configDotenv (options) {
// Specifying an encoding returns a string instead of a buffer
const parsed = DotenvModule.parse(fs.readFileSync(dotenvPath, { encoding }))

DotenvModule.populate(process.env, parsed, options)
let processEnv = process.env
if (options && options.processEnv != null) {
processEnv = options.processEnv
}

DotenvModule.populate(processEnv, parsed, options)

return { parsed }
} catch (e) {
Expand Down
15 changes: 15 additions & 0 deletions tests/test-config-vault.js
Expand Up @@ -194,6 +194,21 @@ t.test('does write over keys already in process.env if override turned on', ct =
ct.equal(process.env.ALPHA, 'zeta')
})

t.test('can write to a different object rather than process.env', ct => {
ct.plan(3)

process.env.ALPHA = 'other' // reset process.env

logStub = sinon.stub(console, 'log')

const myObject = {}

const result = dotenv.config({ path: testPath, processEnv: myObject })
ct.equal(result.parsed.ALPHA, 'zeta')
ct.equal(process.env.ALPHA, 'other')
ct.equal(myObject.ALPHA, 'zeta')
})

t.test('logs when debug and override are turned on', ct => {
ct.plan(1)

Expand Down
14 changes: 14 additions & 0 deletions tests/test-config.js
Expand Up @@ -135,6 +135,20 @@ t.test(
}
)

t.test('can write to a different object rather than process.env', ct => {
ct.plan(3)

process.env.test = 'other' // reset process.env

const myObject = {}
const env = dotenv.config({ processEnv: myObject })

ct.equal(env.parsed && env.parsed.test, mockParseResponse.test)
console.log('logging', process.env.test)
ct.equal(process.env.test, 'other')
ct.equal(myObject.test, mockParseResponse.test)
})

t.test('returns parsed object', ct => {
ct.plan(2)

Expand Down

0 comments on commit 406f2c3

Please sign in to comment.