mirror of
https://github.com/toddbluhm/env-cmd.git
synced 2025-12-08 18:23:33 +00:00
Fixed coverage and updated Readme file
This commit is contained in:
parent
e4947984b9
commit
a73616073f
@ -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.
|
||||
|
||||
25
lib/index.js
25
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
|
||||
|
||||
32
test/test.js
32
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 () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user