Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dotenvx/dotenvx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.42.0
Choose a base ref
...
head repository: dotenvx/dotenvx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.43.0
Choose a head ref
  • 5 commits
  • 11 files changed
  • 1 contributor

Commits on May 20, 2024

  1. rewrite .config to use Run under the hood

    motdotla committed May 20, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    dtolnay David Tolnay
    Copy the full SHA
    70fcafa View commit details
  2. clean up tests

    motdotla committed May 20, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    dtolnay David Tolnay
    Copy the full SHA
    d59a8f7 View commit details
  3. changelog 🪵

    motdotla committed May 20, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    dtolnay David Tolnay
    Copy the full SHA
    cf4e9de View commit details
  4. Merge pull request #225 from dotenvx/require-decrypt

    rewrite `.config` to use `Run` under the hood
    motdotla authored May 20, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    dtolnay David Tolnay
    Copy the full SHA
    d204c85 View commit details
  5. 0.43.0

    motdotla committed May 20, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    dtolnay David Tolnay
    Copy the full SHA
    61492cb View commit details
22 changes: 14 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,17 +2,23 @@

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/dotenvx/dotenvx/compare/v0.42.0...main)
## [Unreleased](https://github.com/dotenvx/dotenvx/compare/v0.43.0...main)

## 0.43.0

### Added

* Support `require('@dotenvx/dotenvx').config()` for `DOTENV_PRIVATE_KEY` decryption ([#225](https://github.com/dotenvx/dotenvx/pull/225))

## 0.42.0

## Added
### Added

* Added `.env.vault deprecated` warning when using `DOTENV_KEY`. Provide instructions to convert to encrypted `.env` files. ([#224](https://github.com/dotenvx/dotenvx/pull/224))

## 0.41.0

## Added
### Added

* Added `vault convert` command to list convert instructions for converting `.env.vault` to encrypted .env files ([#222](https://github.com/dotenvx/dotenvx/pull/222))

@@ -39,27 +45,27 @@ Update production with your new DOTENV_PRIVATE_KEY_PRODUCTION located in .env.ke
Learn more at [https://dotenvx.com/docs/quickstart#add-encryption]
```

## Changed
### Changed

* Rename `encryptme` to `convert` ([#222](https://github.com/dotenvx/dotenvx/pull/222))

## 0.40.1

## Added
### Added

* Support encryption replacemnt of multiline values ([#220](https://github.com/dotenvx/dotenvx/pull/220))

## 0.40.0

## Added
### Added

* Added `dotenvx encryptme` command to convert an entire `.env` file to an encrypted `.env` file. ([#213](https://github.com/dotenvx/dotenvx/pull/213))

## Changed
### Changed

* Made `precommit` smart enough to check if a `.env*` file is encrypted or not. If fully encrypted, then allow `precommit` check to pass ([#211](https://github.com/dotenvx/dotenvx/pull/211))

## Removed
### Removed

* Do not warn of missing files for conventions (too noisy) ([#216](https://github.com/dotenvx/dotenvx/pull/216))

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.42.0",
"version": "0.43.0",
"name": "@dotenvx/dotenvx",
"description": "a better dotenv–from the creator of `dotenv`",
"author": "@motdotla",
21 changes: 21 additions & 0 deletions src/lib/helpers/dotenvOptionPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const resolveHome = require('./resolveHome')

function dotenvOptionPaths (options) {
let optionPaths = ['.env']

if (options && options.path) {
if (!Array.isArray(options.path)) {
optionPaths = [resolveHome(options.path)]
} else {
optionPaths = [] // reset default

for (const filepath of options.path) {
optionPaths.push(resolveHome(filepath))
}
}
}

return optionPaths
}

module.exports = dotenvOptionPaths
11 changes: 4 additions & 7 deletions src/lib/helpers/parseDecryptEvalExpand.js
Original file line number Diff line number Diff line change
@@ -7,13 +7,10 @@ function parseDecryptEvalExpand (src, privateKey = null) {
// parse
const parsed = dotenv.parse(src)

// inline decrypt
for (const key in parsed) {
const value = parsed[key]

// handle inline encrypted values
if (privateKey && privateKey.length > 0) {
// privateKey
// handle inline encrypted values
if (privateKey && privateKey.length > 0) {
for (const key in parsed) {
const value = parsed[key]
parsed[key] = decryptValue(value, privateKey)
}
}
12 changes: 12 additions & 0 deletions src/lib/helpers/resolveHome.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const os = require('os')
const path = require('path')

function resolveHome (filepath) {
if (filepath[0] === '~') {
return path.join(os.homedir(), filepath.slice(1))
}

return filepath
}

module.exports = resolveHome
121 changes: 108 additions & 13 deletions src/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const path = require('path')
const logger = require('./../shared/logger')
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand')

// services
const Ls = require('./services/ls')
const Get = require('./services/get')
const Run = require('./services/run')
const Sets = require('./services/sets')
const Status = require('./services/status')
const Encrypt = require('./services/encrypt')
@@ -13,25 +14,119 @@ const Settings = require('./services/settings')
const VaultEncrypt = require('./services/vaultEncrypt')

// helpers
const dotenvEval = require('./helpers/dotenvEval')
const dotenvOptionPaths = require('./helpers/dotenvOptionPaths')

// proxies to dotenv
const config = function (options) {
const env = dotenv.config(options)
const config = function (options = {}) {
// allow user to set processEnv to write to
let processEnv = process.env
if (options && options.processEnv != null) {
processEnv = options.processEnv
}

// overload
const overload = options.overload || options.override

// if processEnv passed also pass to expand
if (options && options.processEnv) {
env.processEnv = options.processEnv
// DOTENV_KEY
let DOTENV_KEY = process.env.DOTENV_KEY
if (options && options.DOTENV_KEY) {
DOTENV_KEY = options.DOTENV_KEY
}
const expanded = dotenvExpand.expand(env)

// if processEnv passed also pass to eval
if (options && options.processEnv) {
expanded.processEnv = options.processEnv
// debug -> log level
if (options && options.debug) {
logger.level = 'debug'
logger.debug('setting log level to debug')
}
const evaluated = dotenvEval.eval(expanded)

return evaluated
// build envs using user set option.path
const optionPaths = dotenvOptionPaths(options) // [ '.env' ]

try {
const envs = []
for (const optionPath of optionPaths) {
// if DOTENV_KEY is set then assume we are checking envVaultFile
if (DOTENV_KEY) {
envs.push({ type: 'envVaultFile', value: path.join(path.dirname(optionPath), '.env.vault') })
} else {
envs.push({ type: 'envFile', value: optionPath })
}
}

const {
processedEnvs,
readableFilepaths,
uniqueInjectedKeys
} = new Run(envs, overload, DOTENV_KEY, processEnv).run()

let lastError
const parsedAll = {}

for (const processedEnv of processedEnvs) {
if (processedEnv.type === 'envVaultFile') {
logger.verbose(`loading env from encrypted ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`)
logger.debug(`decrypting encrypted env from ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`)
}

if (processedEnv.type === 'envFile') {
logger.verbose(`loading env from ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`)
}

if (processedEnv.error) {
lastError = processedEnv.error

if (processedEnv.error.code === 'MISSING_ENV_FILE') {
// do not warn for conventions (too noisy)
if (!options.convention) {
logger.warnv(processedEnv.error)
logger.help(`? add one with [echo "HELLO=World" > ${processedEnv.filepath}] and re-run [dotenvx run -- yourcommand]`)
}
} else {
logger.warnv(processedEnv.error)
}
} else {
Object.assign(parsedAll, processedEnv.injected)
Object.assign(parsedAll, processedEnv.preExisted) // preExisted 'wins'

// debug parsed
const parsed = processedEnv.parsed
logger.debug(parsed)

// verbose/debug injected key/value
const injected = processedEnv.injected
for (const [key, value] of Object.entries(injected)) {
logger.verbose(`${key} set`)
logger.debug(`${key} set to ${value}`)
}

// verbose/debug preExisted key/value
const preExisted = processedEnv.preExisted
for (const [key, value] of Object.entries(preExisted)) {
logger.verbose(`${key} pre-exists (protip: use --overload to override)`)
logger.debug(`${key} pre-exists as ${value} (protip: use --overload to override)`)
}
}
}

let msg = `injecting env (${uniqueInjectedKeys.length})`
if (readableFilepaths.length > 0) {
msg += ` from ${readableFilepaths.join(', ')}`
}
logger.successv(msg)

if (lastError) {
return { parsed: parsedAll, error: lastError }
} else {
return { parsed: parsedAll }
}
} catch (error) {
logger.error(error.message)
if (error.help) {
logger.help(error.help)
}

return { parsed: {}, error }
}
}

const configDotenv = function (options) {
6 changes: 3 additions & 3 deletions tests/lib/config-expand.test.js
Original file line number Diff line number Diff line change
@@ -23,14 +23,14 @@ t.test('expands', ct => {
ct.end()
})

t.test('expands using machine value', ct => {
t.test('expands using the file value first (if it exists)', ct => {
process.env.MACHINE = 'machine'

const testPath = 'tests/.env.expand'
const env = dotenvx.config({ path: testPath })

ct.equal(env.parsed.MACHINE_EXPAND, 'machine')
ct.equal(process.env.MACHINE_EXPAND, 'machine')
ct.equal(env.parsed.MACHINE_EXPAND, 'machine_env')
ct.equal(process.env.MACHINE_EXPAND, 'machine_env')

ct.end()
})
Loading