dotenv-expand
Advanced tools
Comparing version 5.1.0 to 6.0.0
'use strict' | ||
var dotenvExpand = function (config) { | ||
// if ignoring process.env, use a blank object | ||
var environment = config.ignoreProcessEnv ? {} : process.env | ||
function _interpolate (envValue, environment, config) { | ||
const matches = envValue.match(/(.?\${*[\w]*(?::-)?[\w]*}*)/g) || [] | ||
var interpolate = function (envValue) { | ||
var matches = envValue.match(/(.?\${?(?:[a-zA-Z0-9_]+)?}?)/g) || [] | ||
return matches.reduce(function (newEnv, match, index) { | ||
const parts = /(.?)\${*([\w]*(?::-)?[\w]*)?}*/g.exec(match) | ||
if (!parts || parts.length === 0) { | ||
return newEnv | ||
} | ||
return matches.reduce(function (newEnv, match) { | ||
var parts = /(.?)\${?([a-zA-Z0-9_]+)?}?/g.exec(match) | ||
var prefix = parts[1] | ||
const prefix = parts[1] | ||
var value, replacePart | ||
let value, replacePart | ||
if (prefix === '\\') { | ||
replacePart = parts[0] | ||
value = replacePart.replace('\\$', '$') | ||
} else { | ||
var key = parts[2] | ||
replacePart = parts[0].substring(prefix.length) | ||
// process.env value 'wins' over .env file's value | ||
value = environment.hasOwnProperty(key) ? environment[key] : (config.parsed[key] || '') | ||
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] || '') | ||
// Resolve recursive interpolations | ||
value = interpolate(value) | ||
// If the value is found, remove nested expansions. | ||
if (keyParts.length > 1 && value) { | ||
const replaceNested = matches[index + 1] | ||
matches[index + 1] = '' | ||
newEnv = newEnv.replace(replaceNested, '') | ||
} | ||
// Resolve recursive interpolations | ||
value = _interpolate(value, environment, config) | ||
} | ||
return newEnv.replace(replacePart, value) | ||
}, envValue) | ||
} | ||
return newEnv.replace(replacePart, value) | ||
}, envValue) | ||
} | ||
for (var configKey in config.parsed) { | ||
var value = environment.hasOwnProperty(configKey) ? environment[configKey] : config.parsed[configKey] | ||
function expand (config) { | ||
// if ignoring process.env, use a blank object | ||
const environment = config.ignoreProcessEnv ? {} : process.env | ||
config.parsed[configKey] = interpolate(value) | ||
for (const configKey in config.parsed) { | ||
const value = Object.prototype.hasOwnProperty.call(environment, configKey) ? environment[configKey] : config.parsed[configKey] | ||
config.parsed[configKey] = _interpolate(value, environment, config) | ||
} | ||
for (var processKey in config.parsed) { | ||
for (const processKey in config.parsed) { | ||
environment[processKey] = config.parsed[processKey] | ||
@@ -46,2 +60,2 @@ } | ||
module.exports = dotenvExpand | ||
module.exports.expand = expand |
{ | ||
"name": "dotenv-expand", | ||
"version": "5.1.0", | ||
"version": "6.0.0", | ||
"description": "Expand environment variables using dotenv", | ||
"main": "lib/main.js", | ||
"types": "lib/main.d.ts", | ||
"exports": { | ||
".": { | ||
"require": "./lib/main.js", | ||
"types": "./lib/main.d.ts", | ||
"default": "./lib/main.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"scripts": { | ||
"test": "lab test/* --coverage", | ||
"posttest": "npm run lint", | ||
"lint": "standard" | ||
"dts-check": "tsc --project tests/types/tsconfig.json", | ||
"lint": "standard", | ||
"pretest": "npm run lint && npm run dts-check", | ||
"test": "lab tests/* --coverage" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/motdotla/dotenv-expand" | ||
}, | ||
"author": "motdotla", | ||
"keywords": [ | ||
"dotenv", | ||
"expand", | ||
"variables" | ||
"variables", | ||
"env", | ||
".env" | ||
], | ||
"author": "motdotla", | ||
"readmeFilename": "README.md", | ||
"license": "BSD-2-Clause", | ||
"devDependencies": { | ||
"dotenv": "^4.0.0", | ||
"lab": "^13.0.1", | ||
"@types/node": "^17.0.8", | ||
"dotenv": "^14.1.0", | ||
"lab": "^14.3.4", | ||
"should": "^11.2.1", | ||
"standard": "^9.0.2" | ||
"standard": "^16.0.4", | ||
"typescript": "^4.5.4" | ||
}, | ||
"types": "./index.d.ts" | ||
"engines": { | ||
"node": ">=12" | ||
} | ||
} |
@@ -0,1 +1,5 @@ | ||
<p align="center"> | ||
<strong>Announcement 📣</strong><br/>From the makers that brought you Dotenv, introducing <a href="https://sync.dotenv.org">Dotenv Sync</a>.<br/>Sync your .env files between machines, environments, and team members.<br/><a href="https://sync.dotenv.org">Join the early access list.💛</a> | ||
</p> | ||
# dotenv-expand | ||
@@ -17,2 +21,3 @@ | ||
```bash | ||
# Install locally (recommended) | ||
npm install dotenv --save | ||
@@ -22,7 +27,23 @@ npm install dotenv-expand --save | ||
Or installing with yarn? `yarn add dotenv-expand` | ||
## Usage | ||
As early as possible in your application, require dotenv and dotenv-expand, and | ||
wrap dotenv-expand around dotenv. | ||
Usage is a cinch! | ||
### 1. Create a .env file with variable expansions in the root directory of your project | ||
```dosini | ||
# .env file | ||
# | ||
# Add environment-specific variables on new lines in the form of NAME=VALUE | ||
# | ||
PASSWORD=s1mpl3 | ||
DB_HOST=localhost | ||
DB_USER=root | ||
DB_PASS=$PASSWORD | ||
``` | ||
### 2. As early as possible in your application, import dotenv and expand with dotenv-expand | ||
```js | ||
@@ -33,7 +54,62 @@ var dotenv = require('dotenv') | ||
var myEnv = dotenv.config() | ||
dotenvExpand(myEnv) | ||
dotenvExpand.expand(myEnv) | ||
console.log(process.env) | ||
``` | ||
See [test/.env](./test/.env) for examples of variable expansion in your `.env` | ||
file. | ||
### 3. That's it! 👏 | ||
`process.env` now has the expanded keys and values you defined in your `.env` file. | ||
## Examples | ||
See [test/.env](https://github.com/motdotla/dotenv-expand/blob/master/test/.env) for simple and complex examples of variable expansion in your `.env` | ||
file. | ||
## Documentation | ||
DotenvExpand exposes one function: | ||
* expand | ||
### Expand | ||
`expand` will expand your environment variables. | ||
```js | ||
const dotenv = { | ||
parsed: { | ||
BASIC: 'basic', | ||
BASIC_EXPAND: '${BASIC}', | ||
BASIC_EXPAND_SIMPLE: '$BASIC' | ||
} | ||
} | ||
const obj = dotenvExpand.expand(dotenv) | ||
console.log(obj) | ||
``` | ||
#### Options | ||
##### ignoreProcessEnv | ||
Default: `false` | ||
Turn off writing to `process.env`. | ||
```js | ||
const dotenv = { | ||
ignoreProcessEnv: true, | ||
parsed: { | ||
SHOULD_NOT_EXIST: 'testing' | ||
} | ||
} | ||
const obj = dotenvExpand.expand(dotenv).parsed | ||
console.log(obj.SHOULD_NOT_EXIST) // testing | ||
console.log(process.env.SHOULD_NOT_EXIST) // undefined | ||
``` | ||
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
72
113
0
8431
6