From a37f9430b9db24d1d4cbc987642ffa482bbd7148 Mon Sep 17 00:00:00 2001 From: Russell Matney Date: Fri, 12 Sep 2014 11:23:32 -0400 Subject: [PATCH] tests for .env.json files, README updates --- README.md | 20 +++++++++++++------- test/mock-env-json.json | 5 +++++ test/{mock-env => mock-env-module} | 0 test/test-gulp-env.js | 27 +++++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 test/mock-env-json.json rename test/{mock-env => mock-env-module} (100%) diff --git a/README.md b/README.md index 0a65c8a..6230db4 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -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']) @@ -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] @@ -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 diff --git a/test/mock-env-json.json b/test/mock-env-json.json new file mode 100644 index 0000000..4a05085 --- /dev/null +++ b/test/mock-env-json.json @@ -0,0 +1,5 @@ +{ + "STARK": "direwolf", + "BARATHEON": "stag", + "LANNISTER": "lion" +} diff --git a/test/mock-env b/test/mock-env-module similarity index 100% rename from test/mock-env rename to test/mock-env-module diff --git a/test/test-gulp-env.js b/test/test-gulp-env.js index 522cdcf..716ec4e 100644 --- a/test/test-gulp-env.js +++ b/test/test-gulp-env.js @@ -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"); + }); + })