Skip to content

Commit

Permalink
Fixed coverage and updated Readme file
Browse files Browse the repository at this point in the history
  • Loading branch information
toddbluhm committed Sep 21, 2017
1 parent e494798 commit a736160
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`**

Expand All @@ -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.
Expand Down
25 changes: 18 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ describe('env-cmd', function () {
"development": {
"BOB": "COOL",
"NODE_ENV": "dev",
"ANSWER": "42"
"ANSWER": "42",
"TEST_CASES": true
},
"production": {
"BOB": "COOL",
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit a736160

Please sign in to comment.