Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional export keyword #214

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ line'}
```
- inner quotes are maintained (think JSON) (`JSON={"foo": "bar"}` becomes `{JSON:"{\"foo\": \"bar\"}"`)
- whitespace is removed from both ends of the value (see more on [`trim`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim)) (`FOO=" some value "` becomes `{FOO: 'some value'}`)
- optional `export` keyword is ignored (`export FOO=bar` becomes `{FOO: "bar"}`)

> **Note:** Using `export` in your files allows you to easily export all of your variables into the current bash/terminal session by running `source .env`.


## FAQ

Expand Down
2 changes: 1 addition & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function parse (src) {
// convert Buffers before splitting into lines and processing
src.toString().split('\n').forEach(function (line) {
// matching "KEY' and 'VAL' in 'KEY=VAL'
var keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
var keyValueArr = line.match(/^\s*(?:export )?\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
// matched?
if (keyValueArr != null) {
var key = keyValueArr[1]
Expand Down
2 changes: 2 additions & 0 deletions test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ RETAIN_INNER_QUOTES={"foo": "bar"}
RETAIN_INNER_QUOTES_AS_STRING='{"foo": "bar"}'
INCLUDE_SPACE=some spaced out string
USERNAME="therealnerdybeast@example.tld"
export EXPORT_VAR="exported var"
export_foo="is not chopped off"
10 changes: 10 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,15 @@ describe('dotenv', function () {
parsed.should.have.property('USERNAME', 'therealnerdybeast@example.tld')
done()
})

it('parses exported variables properly', function (done) {
parsed.should.have.property('EXPORT_VAR', 'exported var')
done()
})

it('does not chop variable names that start with "export"', function (done) {
parsed.should.have.property('export_foo', 'is not chopped off')
done()
})
})
})