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

Add support for dotenv #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MESSAGE_FROM_DOTENV=Hello, World!
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test.js
.travis.yml
.env
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ b.transform(envify({
}))
```

## Loading `.env` file ##

Envify can load a `.env` file for you using the
[dotenv](https://github.com/motdotla/dotenv) package. To enable this
functionallity, simply use the subarg syntax and include `dotenv` after
`envify`, e.g.:

``` bash
browserify index.js -t [ envify dotenv --NODE_ENV development ]
```

Or if you're using the module API, you can pass `_: "dotenv"` into your
arguments like so:

``` javascript
b.transform(envify({
_: 'dotenv'
, NODE_ENV: 'development'
}))
```

## Contributors ##

* [hughsk](http://github.com/hughsk)
Expand Down
25 changes: 24 additions & 1 deletion custom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
var esprima = require('esprima')
var dotenv = require('dotenv')
, esprima = require('esprima')
, fs = require('fs')
, through = require('through')

var processEnvPattern = /\bprocess\.env\b/

var dotenvCache
function getDotenv() {
if (dotenvCache === undefined) {
try {
dotenvCache = dotenv.parse(fs.readFileSync('.env'))
} catch (err) {
if (err.code === 'ENOENT') {
dotenvCache = {}
} else {
throw err
}
}
}

return dotenvCache
}

module.exports = function(rootEnv) {
rootEnv = rootEnv || process.env || {}

Expand All @@ -24,6 +43,10 @@ module.exports = function(rootEnv) {
var purge = args.indexOf('purge') !== -1
var replacements = []

if (args.indexOf('dotenv') !== -1) {
envs.push(getDotenv())
}

function match(node) {
return (
node.type === Syntax.MemberExpression
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"tape": "^4.6.0"
},
"dependencies": {
"dotenv": "^4.0.0",
"esprima": "^4.0.0",
"through": "~2.3.4"
},
Expand Down
26 changes: 26 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,29 @@ test('-t [ envify purge --argument ]', function(t) {
, 'var y = process.env.argument'
].join('\n'))
})

test('-t [ envify dotenv ]', function(t) {
var stream = envify({ argument: 'not purged' })
var buffer = ''

stream(__filename, { _: ['dotenv'] })
.on('data', function(d) { buffer += d })
.on('end', function() {
t.notEqual(-1, buffer.indexOf('var x = "Hello, World!"'), 'replaces with value from dotenv file')
t.end()
})
.end('var x = process.env.MESSAGE_FROM_DOTENV')
})

test('-t [ envify dotenv --MESSAGE_FROM_DOTENV intercepted ]', function(t) {
var stream = envify({ argument: 'not purged' })
var buffer = ''

stream(__filename, { _: ['dotenv'], MESSAGE_FROM_DOTENV: 'intercepted' })
.on('data', function(d) { buffer += d })
.on('end', function() {
t.notEqual(-1, buffer.indexOf('var x = "intercepted"'), 'uses arguments before dotenv file')
t.end()
})
.end('var x = process.env.MESSAGE_FROM_DOTENV')
})