Merge pull request #17 from serapath-contribution/master

ADD default .env file as a fallback
This commit is contained in:
Todd Bluhm 2017-03-02 17:30:01 -05:00 committed by GitHub
commit 437258f287
4 changed files with 46 additions and 7 deletions

View File

@ -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`**

View File

@ -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()

View File

@ -28,7 +28,8 @@
"author": "Todd Bluhm",
"contributors": [
"Eric Lanehart <eric@pushred.co>",
"Jon Scheiding <jonscheiding@gmail.com>"
"Jon Scheiding <jonscheiding@gmail.com>",
"serapath (Alexander Praetorius) <dev@serapath.de>"
],
"license": "MIT",
"bugs": {

View File

@ -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')