From a73616073fa38d7437e37b02b446da3832f7de60 Mon Sep 17 00:00:00 2001 From: Todd Bluhm Date: Thu, 21 Sep 2017 17:12:18 -0500 Subject: [PATCH] Fixed coverage and updated Readme file --- README.md | 6 +++++- lib/index.js | 25 ++++++++++++++++++------- test/test.js | 32 +++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5a26e1f..7ec5ae1 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ or ### .rc file usage -For more complex projects, a `.env-cmdrc` file can be defined in the root directory and supports as many environments as you want. Instead of passing the path to a `.env` file to `env-cmd`, simply pass the name of the environment you want to use thats in your `.env-cmdrc` file. +For more complex projects, a `.env-cmdrc` file can be defined in the root directory and supports as many environments as you want. Instead of passing the path to a `.env` file to `env-cmd`, simply pass the name of the environment you want to use thats in your `.env-cmdrc` file. You may also use multiple environment names to merge env vars together. **.rc file `.env-cmdrc`** @@ -97,7 +97,11 @@ For more complex projects, a `.env-cmdrc` file can be defined in the root direct **Terminal** ```sh ./node_modules/.bin/env-cmd production node index.js +# Or for multiple environments (where `production` vars override `common` vars, +# but both are included) +./node_modules/.bin/env-cmd common,production node index.js ``` + ### --no-override option Sometimes you want to set env variables from a file without overriding existing process env vars. diff --git a/lib/index.js b/lib/index.js index f19d2c4..50ae520 100644 --- a/lib/index.js +++ b/lib/index.js @@ -119,14 +119,25 @@ function UseRCFile (parsedArgs) { const fileData = fs.readFileSync(rcFileLocation, { encoding: 'utf8' }) const parsedData = ParseRCFile(fileData) - let result = {}; - const envNames = parsedArgs.envFile.split(','); - envNames.forEach(function(name) { + let result = {} + const envNames = parsedArgs.envFile.split(',') + + if (envNames.length === 1 && !parsedData[envNames[0]]) { + console.error(`Error: + Could not find environment: + ${parsedArgs.envFile} + in .rc file: + ${rcFileLocation}`) + throw new Error(`Missing environment ${parsedArgs.envFile} in .env-cmdrc file.`) + } + + envNames.forEach(function (name) { const envVars = parsedData[name] - if (envVars) - result = Object.assign(result, envVars); - }); - return result; + if (envVars) { + result = Object.assign(result, envVars) + } + }) + return result } // Uses the cli passed env file to get env vars diff --git a/test/test.js b/test/test.js index b5c38f7..5a92377 100644 --- a/test/test.js +++ b/test/test.js @@ -163,7 +163,8 @@ describe('env-cmd', function () { "development": { "BOB": "COOL", "NODE_ENV": "dev", - "ANSWER": "42" + "ANSWER": "42", + "TEST_CASES": true }, "production": { "BOB": "COOL", @@ -210,6 +211,35 @@ describe('env-cmd', function () { assert(e.message.includes(`.env-cmdrc`)) } }) + + it('should parse env vars from .env-cmdrc file using both development and production env', function () { + EnvCmd(['development,production', 'echo', '$BOB']) + assert(spawnStub.args[0][0] === 'echo') + assert(spawnStub.args[0][1][0] === '$BOB') + assert(spawnStub.args[0][2].env.BOB === 'COOL') + assert(spawnStub.args[0][2].env.NODE_ENV === 'prod') + assert(spawnStub.args[0][2].env.ANSWER === '43') + assert(spawnStub.args[0][2].env.TEST_CASES === true) + }) + + it('should parse env vars from .env-cmdrc file using both development and production env in reverse order', function () { + EnvCmd(['production,development', 'echo', '$BOB']) + assert(spawnStub.args[0][0] === 'echo') + assert(spawnStub.args[0][1][0] === '$BOB') + assert(spawnStub.args[0][2].env.BOB === 'COOL') + assert(spawnStub.args[0][2].env.NODE_ENV === 'dev') + assert(spawnStub.args[0][2].env.ANSWER === '42') + assert(spawnStub.args[0][2].env.TEST_CASES === true) + }) + + it('should not fail if only one environment name exists', function () { + EnvCmd(['production,test', 'echo', '$BOB']) + assert(spawnStub.args[0][0] === 'echo') + assert(spawnStub.args[0][1][0] === '$BOB') + assert(spawnStub.args[0][2].env.BOB === 'COOL') + assert(spawnStub.args[0][2].env.NODE_ENV === 'prod') + assert(spawnStub.args[0][2].env.ANSWER === '43') + }) }) describe('EnvCmd', function () {