dotenv-expand
Advanced tools
Comparing version 9.0.0 to 10.0.0
@@ -5,10 +5,16 @@ # Changelog | ||
## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v9.0.0...master) | ||
## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v10.0.0...master) | ||
## [9.0.0](https://github.com/motdotla/dotenv-expand/compare/v8.0.3...v9.0.0) (2022-03-21) | ||
## [10.0.0](https://github.com/motdotla/dotenv-expand/compare/v9.0.0...v10.0.0) (2022-12-16) | ||
### Added | ||
- Proper support for preload and cli args ([#78])(https://github.com/motdotla/dotenv-expand/pull/78) | ||
- Support special characters in default expansion ([#74](https://github.com/motdotla/dotenv-expand/pull/74)) | ||
## [9.0.0](https://github.com/motdotla/dotenv-expand/compare/v8.0.3...v9.0.0) (2022-08-30) | ||
### Added | ||
- Proper support for preload and cli args ([#78](https://github.com/motdotla/dotenv-expand/pull/78)) | ||
## [8.0.3](https://github.com/motdotla/dotenv-expand/compare/v8.0.2...v8.0.3) (2022-03-21) | ||
@@ -15,0 +21,0 @@ |
'use strict' | ||
// like String.prototype.search but returns the last index | ||
function _searchLast (str, rgx) { | ||
const matches = Array.from(str.matchAll(rgx)) | ||
return matches.length > 0 ? matches.slice(-1)[0].index : -1 | ||
} | ||
function _interpolate (envValue, environment, config) { | ||
const matches = envValue.match(/(.?\${*[\w]*(?::-[\w/]*)?}*)/g) || [] | ||
// find the last unescaped dollar sign in the | ||
// value so that we can evaluate it | ||
const lastUnescapedDollarSignIndex = _searchLast(envValue, /(?!(?<=\\))\$/g) | ||
return matches.reduce(function (newEnv, match, index) { | ||
const parts = /(.?)\${*([\w]*(?::-[\w/]*)?)?}*/g.exec(match) | ||
if (!parts || parts.length === 0) { | ||
return newEnv | ||
} | ||
// If we couldn't match any unescaped dollar sign | ||
// let's return the string as is | ||
if (lastUnescapedDollarSignIndex === -1) return envValue | ||
const prefix = parts[1] | ||
// This is the right-most group of variables in the string | ||
const rightMostGroup = envValue.slice(lastUnescapedDollarSignIndex) | ||
let value, replacePart | ||
/** | ||
* This finds the inner most variable/group divided | ||
* by variable name and default value (if present) | ||
* ( | ||
* (?!(?<=\\))\$ // only match dollar signs that are not escaped | ||
* {? // optional opening curly brace | ||
* ([\w]+) // match the variable name | ||
* (?::-([^}\\]*))? // match an optional default value | ||
* }? // optional closing curly brace | ||
* ) | ||
*/ | ||
const matchGroup = /((?!(?<=\\))\${?([\w]+)(?::-([^}\\]*))?}?)/ | ||
const match = rightMostGroup.match(matchGroup) | ||
if (prefix === '\\') { | ||
replacePart = parts[0] | ||
value = replacePart.replace('\\$', '$') | ||
} else { | ||
const keyParts = parts[2].split(':-') | ||
const key = keyParts[0] | ||
replacePart = parts[0].substring(prefix.length) | ||
// process.env value 'wins' over .env file's value | ||
value = Object.prototype.hasOwnProperty.call(environment, key) | ||
? environment[key] | ||
: (config.parsed[key] || keyParts[1] || '') | ||
if (match != null) { | ||
const [, group, variableName, defaultValue] = match | ||
// If the value is found, remove nested expansions. | ||
if (keyParts.length > 1 && value) { | ||
const replaceNested = matches[index + 1] | ||
matches[index + 1] = '' | ||
return _interpolate( | ||
envValue.replace( | ||
group, | ||
environment[variableName] || | ||
defaultValue || | ||
config.parsed[variableName] || | ||
'' | ||
), | ||
environment, | ||
config | ||
) | ||
} | ||
newEnv = newEnv.replace(replaceNested, '') | ||
} | ||
// Resolve recursive interpolations | ||
value = _interpolate(value, environment, config) | ||
} | ||
return envValue | ||
} | ||
return newEnv.replace(replacePart, value) | ||
}, envValue) | ||
function _resolveEscapeSequences (value) { | ||
return value.replace(/\\\$/g, '$') | ||
} | ||
@@ -48,5 +63,9 @@ | ||
for (const configKey in config.parsed) { | ||
const value = Object.prototype.hasOwnProperty.call(environment, configKey) ? environment[configKey] : config.parsed[configKey] | ||
const value = Object.prototype.hasOwnProperty.call(environment, configKey) | ||
? environment[configKey] | ||
: config.parsed[configKey] | ||
config.parsed[configKey] = _interpolate(value, environment, config) | ||
config.parsed[configKey] = _resolveEscapeSequences( | ||
_interpolate(value, environment, config) | ||
) | ||
} | ||
@@ -53,0 +72,0 @@ |
{ | ||
"name": "dotenv-expand", | ||
"version": "9.0.0", | ||
"version": "10.0.0", | ||
"description": "Expand environment variables using dotenv", | ||
@@ -38,4 +38,5 @@ "main": "lib/main.js", | ||
"devDependencies": { | ||
"@hapi/lab": "^24.5.1", | ||
"@types/node": "^17.0.8", | ||
"dotenv": "16.0.2", | ||
"dotenv": "16.0.3", | ||
"lab": "^14.3.4", | ||
@@ -42,0 +43,0 @@ "should": "^11.2.1", |
@@ -0,1 +1,48 @@ | ||
<div align="center"> | ||
<p> | ||
<sup> | ||
<a href="https://github.com/sponsors/motdotla">Dotenv libraries are supported by the community.</a> | ||
</sup> | ||
</p> | ||
<sup>Special thanks to:</sup> | ||
<br> | ||
<br> | ||
<a href="https://www.warp.dev/?utm_source=github&utm_medium=referral&utm_campaign=dotenv_p_20220831"> | ||
<div> | ||
<img src="https://res.cloudinary.com/dotenv-org/image/upload/v1661980709/warp_hi8oqj.png" width="230" alt="Warp"> | ||
</div> | ||
<b>Warp is a blazingly fast, Rust-based terminal reimagined to work like a modern app.</b> | ||
<div> | ||
<sup>Get more done in the CLI with real text editing, block-based output, and AI command search.</sup> | ||
</div> | ||
</a> | ||
<br> | ||
<a href="https://retool.com/?utm_source=sponsor&utm_campaign=dotenv"> | ||
<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"> | ||
</div> | ||
<b>Retool helps developers build custom internal software, like CRUD apps and admin panels, really fast.</b> | ||
<div> | ||
<sup>Build UIs visually with flexible components, connect to any data source, and write business logic in JavaScript.</sup> | ||
</div> | ||
</a> | ||
<br> | ||
<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_400/v1665605496/68747470733a2f2f73696e647265736f726875732e636f6d2f6173736574732f7468616e6b732f776f726b6f732d6c6f676f2d77686974652d62672e737667_zdmsbu.svg" width="270" alt="WorkOS"> | ||
</div> | ||
<b>Your App, Enterprise Ready.</b> | ||
<div> | ||
<sup>Add Single Sign-On, Multi-Factor Auth, and more, in minutes instead of months.</sup> | ||
</div> | ||
</a> | ||
<hr> | ||
<br> | ||
<br> | ||
<br> | ||
<br> | ||
</div> | ||
[![dotenv-vault](https://badge.dotenv.org/works-with.svg?r=1)](https://www.dotenv.org/r/github.com/dotenv-org/dotenv-vault?r=1) | ||
@@ -52,4 +99,3 @@ | ||
You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#cli_r_require_module) to preload dotenv & dotenv- | ||
. By doing this, you do not need to require and load dotenv or dotenv-expand in your application code. This is the preferred approach when using `import` instead of `require`. | ||
You can use the `--require` (`-r`) [command line option](https://nodejs.org/api/cli.html#cli_r_require_module) to preload dotenv & dotenv-expand. By doing this, you do not need to require and load dotenv or dotenv-expand in your application code. This is the preferred approach when using `import` instead of `require`. | ||
@@ -56,0 +102,0 @@ ```bash |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13899
101
195
7