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`** diff --git a/lib/index.js b/lib/index.js index e43a560..426d5a4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,6 +4,7 @@ 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 @@ -98,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) @@ -133,8 +134,18 @@ 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(`WARNING: + Could not find or read file at: + ${envFilePath} + Trying to fallback to read: + ${envFilePathDefault} + `) + 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() 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')