env-cmd
Advanced tools
Comparing version 4.0.0 to 5.0.0
# Changelog | ||
## 5.0.0 | ||
- ***BREAKING***: Inline comments are no longer allowed in `.env` files (full line comments are still allowed) | ||
- ***BREAKING***: `.env` file no longer supports the `env var` format (only `env=var` is allowed now) | ||
- ***BREAKING***: Double Quotes are no longer needed when using special symbols (such as `#`) in the value portion of an env var | ||
- **Feature**: if the given env file cannot be found, it will auto default to searching | ||
the execution directory for a file called `.env` and use that as a fallback. See README for why this is | ||
helpful. (special thanks to Alexander Praetorius) | ||
## 4.0.0 | ||
@@ -4,0 +12,0 @@ - ***BREAKING***: In order to use double quotes as part of the env value, you must now surround those double quotes with an additional set of quotes: So `ENV1="value"` -> `ENV1=""value""` (this only applies to double quotes, single quotes continue to work as normal) |
@@ -7,2 +7,3 @@ 'use strict' | ||
const rcFileLocation = path.join(process.cwd(), '.env-cmdrc') | ||
const envFilePathDefault = path.join(process.cwd(), '.env') | ||
@@ -59,7 +60,7 @@ function EnvCmd (args) { | ||
function StripComments (envString) { | ||
const commentsRegex = /("{1}.*"{1})*?([ ]*#.*$)/gim | ||
const commentsRegex = /(^#.*$)/gim | ||
let match = commentsRegex.exec(envString) | ||
let newString = envString | ||
while (match != null) { | ||
newString = newString.replace(match[2], '') | ||
newString = newString.replace(match[1], '') | ||
match = commentsRegex.exec(envString) | ||
@@ -76,17 +77,5 @@ } | ||
// Stripes out double quotes to allow for usage for special # in values | ||
function StripDoubleQuotes (envString) { | ||
const doubleQuotesRegex = /"{1}(.*)"{1}/gim | ||
let match = doubleQuotesRegex.exec(envString) | ||
let newString = envString | ||
while (match != null) { | ||
newString = newString.replace(match[0], match[1]) | ||
match = doubleQuotesRegex.exec(envString) | ||
} | ||
return newString | ||
} | ||
// Parse out all env vars from an env file string | ||
function ParseEnvVars (envString) { | ||
const envParseRegex = /^((.+?)[ =](.*))$/gim | ||
const envParseRegex = /^((.+?)[=](.*))$/gim | ||
const matches = {} | ||
@@ -104,3 +93,3 @@ let match | ||
// First thing we do is stripe out all comments | ||
envFileString = StripComments(envFileString) | ||
envFileString = StripComments(envFileString.toString()) | ||
@@ -110,5 +99,2 @@ // Next we stripe out all the empty lines | ||
// Finally we stripe out all the double quotes for special charactes | ||
envFileString = StripDoubleQuotes(envFileString) | ||
// Parse the envs vars out | ||
@@ -130,3 +116,12 @@ const envs = ParseEnvVars(envFileString) | ||
const parsedData = ParseRCFile(fileData) | ||
return parsedData[parsedArgs.envFile] | ||
const envVars = parsedData[parsedArgs.envFile] | ||
if (!envVars) { | ||
console.error(`Error: | ||
Could not find environment: | ||
${parsedArgs.envFile} | ||
in .rc file: | ||
${rcFileLocation}`) | ||
throw new Error(`Missing environment ${parsedArgs.envFile} in .env-cmdrc file.`) | ||
} | ||
return envVars | ||
} | ||
@@ -142,6 +137,21 @@ | ||
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} | ||
`) | ||
} | ||
// If we don't have a main file try the fallback file | ||
if (!file) { | ||
try { | ||
file = fs.readFileSync(envFilePathDefault) | ||
} catch (e) { | ||
throw new Error(`Error! Could not fallback to find or read file at ${envFilePathDefault}`) | ||
} | ||
} | ||
// Get the file extension | ||
const ext = path.extname(envFilePath).toLowerCase() | ||
@@ -187,3 +197,2 @@ | ||
StripEmptyLines, | ||
StripDoubleQuotes, | ||
ParseEnvVars, | ||
@@ -190,0 +199,0 @@ ParseRCFile, |
{ | ||
"name": "env-cmd", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Executes a command using the envs in the provided env file", | ||
@@ -31,3 +31,4 @@ "main": "lib/index.js", | ||
"Eric Lanehart <eric@pushred.co>", | ||
"Jon Scheiding <jonscheiding@gmail.com>" | ||
"Jon Scheiding <jonscheiding@gmail.com>", | ||
"serapath (Alexander Praetorius) <dev@serapath.de>" | ||
], | ||
@@ -49,4 +50,4 @@ "license": "MIT", | ||
"sinon": "^1.17.5", | ||
"standard": "^8.6.0" | ||
"standard": "^9.0.0" | ||
} | ||
} |
@@ -12,17 +12,12 @@ [![Travis](https://img.shields.io/travis/toddbluhm/env-cmd.svg)](https://travis-ci.org/toddbluhm/env-cmd) | ||
## Install | ||
`npm install env-cmd` | ||
`npm install env-cmd` or `npm install -g env-cmd` | ||
## Usage | ||
## Basic Usage | ||
**Environment file `./test/.env`** | ||
``` | ||
# This is a comment | ||
ENV1=THANKS # Yay inline comments support | ||
ENV1=THANKS | ||
ENV2=FOR ALL | ||
ENV3 THE FISH # This format is also accepted | ||
# Surround value in double quotes when using a # symbol in the value | ||
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"" | ||
ENV3=THE FISH | ||
``` | ||
@@ -42,8 +37,52 @@ | ||
```sh | ||
# uses ./test/.env | ||
./node_modules/.bin/env-cmd ./test/.env node index.js | ||
``` | ||
## Advanced Usage | ||
### Fallback file usage | ||
You can specify an `.env.local` (or any name) env file, add that to your `.gitignore` and use that in your local development environment. Then you can use a regular `.env` file in root directory with production configs that can get committed to a private/protected repo. When `env-cmd` cannot find the `.env.local` file it will fallback to looking for a regular `.env` file. | ||
**Environment file `./.env.local`** | ||
``` | ||
# This is a comment | ||
ENV1=THANKS | ||
ENV2=FOR ALL | ||
ENV3=THE FISH | ||
``` | ||
**Fallback Environment file `./.env`** | ||
``` | ||
# This can be used as an example fallback | ||
ENV1=foo | ||
ENV2=bar | ||
ENV3=baz | ||
ENV4=quux | ||
ENV5=gorge | ||
``` | ||
**Package.json** | ||
uses `./.env` as a fallback | ||
```json | ||
{ | ||
"scripts": { | ||
"test": "env-cmd ./.env.local mocha -R spec" | ||
} | ||
} | ||
``` | ||
or | ||
**.env-cmdrc file `.env-cmdrc`** | ||
**Terminal** | ||
```sh | ||
# uses ./.env as a fallback, because it can't find `./.env.local` | ||
./node_modules/.bin/env-cmd ./.env.local node index.js | ||
``` | ||
### .rc file usage | ||
For more complex projects, a `.env-cmdrc` file can be defined in the root directory and supports as many environments as you want. Instead of passing the path to a `.env` file to `env-cmd`, simple pass the name of the environment you want use thats in your `.env-cmdrc` file. | ||
**.rc file `.env-cmdrc`** | ||
```json | ||
@@ -61,2 +100,3 @@ { | ||
**Terminal** | ||
```sh | ||
@@ -70,3 +110,2 @@ ./node_modules/.bin/env-cmd production node index.js | ||
- `key=value` | ||
- `key value` | ||
- Key/value pairs as JSON | ||
@@ -94,2 +133,3 @@ - JavaScript file exporting an object | ||
- Jon Scheiding | ||
- Alexander Praetorius | ||
@@ -96,0 +136,0 @@ ## Contributing Guide |
Sorry, the diff of this file is not supported yet
14288
166
137