Skip to content

Commit

Permalink
Merge pull request #2 from moveline/read-json
Browse files Browse the repository at this point in the history
tests for .env.json files, README updates
  • Loading branch information
russmatney committed Sep 12, 2014
2 parents 291c1f2 + a37f943 commit 3372a70
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
gulp-env
========

Add env vars from a .env file to your process.env
Add env vars from a .env or .env.json file to your process.env


Install
Expand All @@ -14,7 +14,14 @@ npm i --save-dev gulp-env
Usage
========

For now, gulp-env expects the .env file to export a javascript object, i.e.:
gulp-env handles two kinds of .env files:

```
//.env.json
{
MONGO_URI: "mongodb://localhost:27017/testdb
}
```

```
//.env
Expand All @@ -24,7 +31,7 @@ module.exports = {
```

You can add the properties of this object to your process.env via
`env({file: ".env})` in your gulpfile.
`env({file: ".env"})` or `env({file: ".env.json"})` in your gulpfile.

```
//gulpfile.js
Expand All @@ -37,7 +44,7 @@ gulp.task('nodemon', function() {
});
gulp.task('set-env', function () {
env({file: ".env"});
env({file: ".env.json"});
});
gulp.task('default', ['set-env', 'nodemon'])
Expand All @@ -55,7 +62,7 @@ Seriously, this is all of the code!
module.exports = function(options) {
if (options.file) {
var env = require("./" + options.file);
var env = require(process.cwd() + "/" + options.file);
for (var prop in env) {
process.env[prop] = env[prop]
Expand All @@ -66,6 +73,5 @@ module.exports = function(options) {

TODO:

- handle non-`module.exports` env files
- handle ini files
- allow for simple variable setting (i.e. `env({PORT: 4000})`, etc.)
- test coverage
5 changes: 5 additions & 0 deletions test/mock-env-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"STARK": "direwolf",
"BARATHEON": "stag",
"LANNISTER": "lion"
}
File renamed without changes.
27 changes: 25 additions & 2 deletions test/test-gulp-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,34 @@ describe('gulp-env', function() {
expect(env).to.exist;
});

it('should add process.env vars from a local file', function() {
env({file: "test/mock-env"})
beforeEach(function() {
delete process.env.STARK;
delete process.env.BARATHEON;
delete process.env.LANNISTER;
});

it('should add process.env vars from a local module', function() {
expect(process.env.STARK).not.to.equal("direwolf");
expect(process.env.BARATHEON).not.to.equal("stag");
expect(process.env.LANNISTER).not.to.equal("lion");

env({file: "test/mock-env-module"})

expect(process.env.STARK).to.equal("direwolf");
expect(process.env.BARATHEON).to.equal("stag");
expect(process.env.LANNISTER).to.equal("lion");
});

it('should add process.env vars from a local json file', function() {
expect(process.env.STARK).not.to.equal("direwolf");
expect(process.env.BARATHEON).not.to.equal("stag");
expect(process.env.LANNISTER).not.to.equal("lion");

env({file: "test/mock-env-json.json"})

expect(process.env.STARK).to.equal("direwolf");
expect(process.env.BARATHEON).to.equal("stag");
expect(process.env.LANNISTER).to.equal("lion");
});

})

0 comments on commit 3372a70

Please sign in to comment.