diff --git a/CHANGELOG.md b/CHANGELOG.md index ffcc10f..4463397 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 3f57d1c..bac3554 100644 --- a/README.md +++ b/README.md @@ -155,15 +155,16 @@ 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' } @@ -171,6 +172,7 @@ const dotenv = { 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 ``` diff --git a/lib/main.js b/lib/main.js index 8f415ce..567e7f3 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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 } @@ -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