From 5c77fc195f517b550dee63e61399cbc52fe8d516 Mon Sep 17 00:00:00 2001 From: Eric Lanehart Date: Tue, 13 Sep 2016 19:22:12 -0400 Subject: [PATCH 1/2] Add support for JSON format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Short circuits custom parser with node’s module loader when given file has a .json extension --- README.md | 1 + lib/index.js | 4 +++- test/test.js | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 517dd63..b253f3a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/index.js b/lib/index.js index f34b10c..1a6fbab 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { diff --git a/test/test.js b/test/test.js index 283ec77..40ee154 100644 --- a/test/test.js +++ b/test/test.js @@ -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') From 0d2f0452a2626b8c80efab64983eec51037391b1 Mon Sep 17 00:00:00 2001 From: Eric Lanehart Date: Thu, 22 Sep 2016 08:25:00 -0400 Subject: [PATCH 2/2] Credit contributors --- README.md | 4 ++++ package.json | 3 +++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index b253f3a..110841c 100644 --- a/README.md +++ b/README.md @@ -54,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 diff --git a/package.json b/package.json index 2d3b50c..55a7701 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,9 @@ "run" ], "author": "Todd Bluhm", + "contributors": [ + "Eric Lanehart " + ], "license": "MIT", "bugs": { "url": "https://github.com/toddbluhm/env-cmd/issues"