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

Warn or Throw on missing env variable #19

Open
wants to merge 2 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ b.transform(envify({
}))
```

## Throw or Warn if env variable is missing ##

May be you want to be sure, that all `process.env` were replaced. There is two
additional subargs to help you with that: `error` and `warn`.

You can use it in the same way as `purge` subarg.

Keep in mind that `purge`, `error` and `warn` are mutual exclusive.

## Contributors ##

* [hughsk](http://github.com/hughsk)
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,39 @@ test('-t [ envify purge --argument ]', function(t) {
, 'var y = process.env.argument'
].join('\n'))
})

test('-t [ envify error ]', function(t) {
var stream = envify()
var buffer = ''
var caughtError = false;

stream(__filename, { _: ['error'] })
.on('data', function(d) { buffer += d })
.on('error', function (e) {
t.ok(e, 'throws error if env variable is missing');
t.end();
})
.on('end', function () {
t.fail('should throw error if env variable is missing')
t.end();
})
.end('var x = process.env.MISSING_VAR')
})

test('-t [ envify error --argument]', function(t) {
var stream = envify()
var buffer = ''
var caughtError = false;

stream(__filename, { _: ['error'], argument: 1 })
.on('data', function(d) { buffer += d })
.on('error', function (e) {
t.fail(e, 'should not throw if env variable is defined');
t.end();
})
.on('end', function () {
t.pass('does not throw error if env variable is defined')
t.end();
})
.end('var x = process.env.argument')
})
7 changes: 7 additions & 0 deletions visitors.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var Syntax = require('jstransform').Syntax
var utils = require('jstransform/src/utils')
var util = require('util')

function create(envs) {
var args = [].concat(envs[0]._ || []).concat(envs[1]._ || [])
var purge = args.indexOf('purge') !== -1
var error = args.indexOf('error') !== -1
var warn = args.indexOf('warn') !== -1

function visitProcessEnv(traverse, node, path, state) {
var key = node.property.name
Expand All @@ -18,6 +21,10 @@ function create(envs) {

if (purge) {
replaceEnv(node, state, undefined)
} else if (error || warn) {
var msg = util.format('envify did not find value for "%s" variable', node.property.name);
if(error) throw new Error(msg);
else console.log('WARN: ', msg);
}

return false
Expand Down