From 1dff4fa2a382cc5ee49d6b8fe512178e3e1ecb60 Mon Sep 17 00:00:00 2001 From: Alexander Praetorius Date: Tue, 21 Feb 2017 09:51:36 +0100 Subject: [PATCH 1/3] ADD default .env file as a fallback If a repository contains a `.env` example file or - in case of a private repository - a `.env` file with production values, a developer can add a local (e.g. `.env.local` file to `.gitignore`) and feed it to `env-cmd`. So in development a custom local configuration can be used, but in production, `env-cmd` falls back to `.env` --- lib/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index e43a560..413cd6e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,7 +4,8 @@ const spawn = require('cross-spawn').spawn const path = require('path') const fs = require('fs') const rcFileLocation = path.join(process.cwd(), '.env-cmdrc') - +const envFilePathDefault = path.join(process.cwd(), '.env') + function EnvCmd (args) { // First Parse the args from the command line const parsedArgs = ParseArgs(args) @@ -133,8 +134,13 @@ function UseCmdLine (parsedArgs) { let file try { file = fs.readFileSync(envFilePath, { encoding: 'utf8' }) - } catch (e) { - throw new Error(`Error! Could not find or read file at ${envFilePath}`) + } catch (err) { + console.error(`Error! Could not find or read file at ${envFilePath}`) + try { + file = fs.readFileSync(envFilePathDefault) + } catch (e) { + throw new Error(`Error! Could not fallback to find or read file at ${envFilePathDefault}`) + } } const ext = path.extname(envFilePath).toLowerCase() From fa24b79ca81d06ddbee1a5049fb36c4d0c7cacc9 Mon Sep 17 00:00:00 2001 From: Alexander Praetorius Date: Tue, 21 Feb 2017 10:00:17 +0100 Subject: [PATCH 2/3] UPDATE readme describe `.env` fallback option --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 400c1b9..67396d4 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,9 @@ A simple node program for executing commands using an environment from an env fi ### Environment File Usage +If the specified environment file can't be found, an error message is logged. +If then `.env` fallback file can't be found too, an error is thrown. + **Environment file `./test/.env`** ``` # This is a comment @@ -28,8 +31,18 @@ ENV4="ValueContains#Symbol" # If using double quotes as part of the value, you must surround the value in double quotes ENV5=""Value includes double quotes"" ``` +**Fallback Environment file `./.env`** +``` +# This can be used as an example fallback +ENV1=foo +ENV2=bar +ENV3=baz +ENV4=quux +ENV5=gorge +``` **Package.json** +to use `./test/.env` ```json { "scripts": { @@ -37,13 +50,26 @@ ENV5=""Value includes double quotes"" } } ``` + +uses `./.env` as a fallback +```json +{ + "scripts": { + "test": "env-cmd ./test/.doesntExist mocha -R spec" + } +} +``` or **Terminal** ```sh +# uses ./test/.env ./node_modules/.bin/env-cmd ./test/.env node index.js +# uses ./.env as a fallback, because i can't find `./test/.myEnv` +./node_modules/.bin/env-cmd ./test/.myEnv node index.js ``` + ### .rc file usage **.rc file `.env-cmdrc`** From 956b0efa8ef3d23298c9bc4f9508be462f186027 Mon Sep 17 00:00:00 2001 From: serapath Date: Tue, 21 Feb 2017 10:30:10 +0100 Subject: [PATCH 3/3] UPDATE testing --- lib/index.js | 11 ++++++++--- package.json | 3 ++- test/test.js | 7 ++++--- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/index.js b/lib/index.js index 413cd6e..426d5a4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,7 +5,7 @@ const path = require('path') const fs = require('fs') const rcFileLocation = path.join(process.cwd(), '.env-cmdrc') const envFilePathDefault = path.join(process.cwd(), '.env') - + function EnvCmd (args) { // First Parse the args from the command line const parsedArgs = ParseArgs(args) @@ -99,7 +99,7 @@ function ParseEnvVars (envString) { // Parse out all env vars from a given env file string and return an object function ParseEnvString (envFileString) { // First thing we do is stripe out all comments - envFileString = StripComments(envFileString) + envFileString = StripComments(envFileString.toString()) // Next we stripe out all the empty lines envFileString = StripEmptyLines(envFileString) @@ -135,7 +135,12 @@ function UseCmdLine (parsedArgs) { try { file = fs.readFileSync(envFilePath, { encoding: 'utf8' }) } catch (err) { - console.error(`Error! Could not find or read file at ${envFilePath}`) + console.error(`WARNING: + Could not find or read file at: + ${envFilePath} + Trying to fallback to read: + ${envFilePathDefault} + `) try { file = fs.readFileSync(envFilePathDefault) } catch (e) { diff --git a/package.json b/package.json index dfa1887..f015b35 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "author": "Todd Bluhm", "contributors": [ "Eric Lanehart ", - "Jon Scheiding " + "Jon Scheiding ", + "serapath (Alexander Praetorius) " ], "license": "MIT", "bugs": { diff --git a/test/test.js b/test/test.js index 7769d43..c68d2bf 100644 --- a/test/test.js +++ b/test/test.js @@ -250,13 +250,14 @@ describe('env-cmd', function () { assert(spawnStub.args[0][2].env.ANSWER === '42') }) - it('should throw error if file does not exist', function () { + it('should throw error if file and fallback does not exist', function () { this.readFileStub.restore() + try { EnvCmd(['./test/.non-existent-file', 'echo', '$BOB']) } catch (e) { - const resolvedPath = path.join(process.cwd(), 'test/.non-existent-file') - assert(e.message === `Error! Could not find or read file at ${resolvedPath}`) + const resolvedPath = path.join(process.cwd(), '.env') + assert(e.message ===`Error! Could not fallback to find or read file at ${resolvedPath}`) return } assert(!'No exception thrown')