Merge pull request #5 from sparkartgroup/json-env

Add support for JSON format
This commit is contained in:
Todd Bluhm 2016-09-23 01:45:03 -04:00 committed by GitHub
commit d46740c56d
4 changed files with 36 additions and 1 deletions

View File

@ -39,6 +39,7 @@ or
These are the currently accepted environment file formats. If any other formats are desired please create an issue.
- `key=value`
- `key value`
- Key/value pairs as JSON
## Why
@ -53,3 +54,7 @@ Because sometimes its just too cumbersome passing lots of environment variables
## Special Thanks
Special thanks to [`cross-env`](https://github.com/kentcdodds/cross-env) for inspiration (use's the same `cross-spawn` lib underneath too).
## Contributors
- Eric Lanehart

View File

@ -17,7 +17,9 @@ function EnvCmd (args) {
}
// Parse the env file string
const env = ParseEnvString(file)
const env = path.extname(parsedArgs.envFilePath).toLowerCase() === '.json'
? Object.assign({}, process.env, require(parsedArgs.envFilePath))
: ParseEnvString(file)
// Execute the command with the given environment variables
if (parsedArgs.command) {

View File

@ -24,6 +24,9 @@
"run"
],
"author": "Todd Bluhm",
"contributors": [
"Eric Lanehart <eric@pushred.co>"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/toddbluhm/env-cmd/issues"

View File

@ -20,6 +20,11 @@ const spawnStub = sinon.spy(() => ({
const lib = proxyquire('../lib', {
'cross-spawn': {
spawn: spawnStub
},
[path.resolve(process.cwd(), 'test/.env.json')]: {
BOB: 'COOL',
NODE_ENV: 'dev',
ANSWER: '42'
}
})
const EnvCmd = lib.EnvCmd
@ -129,6 +134,26 @@ describe('env-cmd', function () {
})
})
describe('JSON format support', function () {
before(function () {
this.readFileStub = sinon.stub(fs, 'readFileSync')
proxyquire.noCallThru()
})
after(function () {
spawnStub.reset()
this.readFileStub.restore()
proxyquire.callThru()
})
it('should parse env vars from JSON with node module loader if file extension is .json', function () {
EnvCmd(['./test/.env.json', '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')
})
})
describe('EnvCmd', function () {
before(function () {
this.readFileStub = sinon.stub(fs, 'readFileSync')