-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into add-node-cli-require-support
- Loading branch information
Showing
15 changed files
with
6,789 additions
and
967 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
coverage/ | ||
examples/ | ||
test/ | ||
tests/ | ||
.editorconfig | ||
.travis.yml | ||
Contributing.md | ||
dotenv-expand.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ node_js: | |
- 5 | ||
- 6 | ||
- 7 | ||
- 8 | ||
- 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Changelog | ||
|
||
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-expand/compare/v6.0.1...master) | ||
|
||
## [6.0.1](/~https://github.com/motdotla/dotenv-expand/compare/v6.0.0...v6.0.1) (2022-01-17) | ||
|
||
### Changed | ||
|
||
- Updated README | ||
|
||
## [6.0.0](/~https://github.com/motdotla/dotenv-expand/compare/v5.1.0...v6.0.0) (2022-01-17) | ||
|
||
### Changed | ||
|
||
- _Breaking_ Move default export to export of `expand` function ([#14b1f2](/~https://github.com/motdotla/dotenv-expand/commit/14b1f28f608bc73450dca8c5aaf3a1e4f65e09ca)) | ||
|
||
### Added | ||
|
||
- Add default expansion 🎉 ([#39](/~https://github.com/motdotla/dotenv-expand/pull/39)) | ||
- Add missing type descriptions | ||
|
||
## 5.1.0 and prior | ||
|
||
Please see commit history. | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// TypeScript Version: 3.0 | ||
/// <reference types="node" /> | ||
|
||
export interface DotenvExpandOptions { | ||
ignoreProcessEnv?: boolean; | ||
error?: Error; | ||
parsed?: { | ||
[name: string]: string; | ||
} | ||
} | ||
|
||
export interface DotenvExpandOutput { | ||
ignoreProcessEnv?: boolean; | ||
error?: Error; | ||
parsed?: { | ||
[name: string]: string; | ||
}; | ||
} | ||
|
||
/** | ||
* Adds variable expansion on top of dotenv. | ||
* | ||
* See https://docs.dotenv.org | ||
* | ||
* @param options - additional options. example: `{ ignoreProcessEnv: false, error: null, parsed: { { KEY: 'value' } }` | ||
* @returns an object with a `parsed` key if successful or `error` key if an error occurred. example: { parsed: { KEY: 'value' } } | ||
* | ||
*/ | ||
export function expand(options?: DotenvExpandOptions): DotenvExpandOutput |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,60 @@ | ||
'use strict' | ||
|
||
var dotenvExpand = function (config) { | ||
// if ignoring process.env, use a blank object | ||
var environment = config.ignoreProcessEnv ? {} : process.env | ||
|
||
var interpolate = function (envValue) { | ||
var matches = envValue.match(/(.?\${?(?:[a-zA-Z0-9_]+)?}?)/g) || [] | ||
|
||
return matches.reduce(function (newEnv, match) { | ||
var parts = /(.?)\${?([a-zA-Z0-9_]+)?}?/g.exec(match) | ||
var prefix = parts[1] | ||
|
||
var 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] || '') | ||
|
||
// Resolve recursive interpolations | ||
value = interpolate(value) | ||
function _interpolate (envValue, environment, config) { | ||
const matches = envValue.match(/(.?\${*[\w]*(?::-)?[\w]*}*)/g) || [] | ||
|
||
return matches.reduce(function (newEnv, match, index) { | ||
const parts = /(.?)\${*([\w]*(?::-)?[\w]*)?}*/g.exec(match) | ||
if (!parts || parts.length === 0) { | ||
return newEnv | ||
} | ||
|
||
const prefix = parts[1] | ||
|
||
let value, replacePart | ||
|
||
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 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) | ||
} | ||
|
||
function expand (config) { | ||
// if ignoring process.env, use a blank object | ||
const environment = config.ignoreProcessEnv ? {} : process.env | ||
|
||
for (var configKey in config.parsed) { | ||
var value = environment.hasOwnProperty(configKey) ? environment[configKey] : config.parsed[configKey] | ||
for (const configKey in config.parsed) { | ||
const value = Object.prototype.hasOwnProperty.call(environment, configKey) ? environment[configKey] : config.parsed[configKey] | ||
|
||
config.parsed[configKey] = interpolate(value) | ||
config.parsed[configKey] = _interpolate(value, environment, config) | ||
} | ||
|
||
for (var processKey in config.parsed) { | ||
for (const processKey in config.parsed) { | ||
environment[processKey] = config.parsed[processKey] | ||
} | ||
|
||
return config | ||
} | ||
|
||
module.exports = dotenvExpand | ||
module.exports.expand = expand |
Oops, something went wrong.