env-cmd
Advanced tools
Comparing version 2.0.0 to 2.1.0
# Changelog | ||
## 2.1.0 | ||
- **Feature**: Added support for `key value` mapping in env vars file | ||
- **Feature**: Added support for inline comments `ENV=VALUE # inline comment` | ||
- **Change**: Will now ignore invalid lines in env vars file instead of throwing an error | ||
- **Change**: Migrated all the parsing over to regex since the file format is simple enough right | ||
now to support that | ||
- **Bug**: Removed old test cases for the `-e/--env` flags that were not needed anymore | ||
## 2.0.0 | ||
@@ -4,0 +12,0 @@ - ***BREAKING***: Removed the `-e` and `--env` flags. Now it just expects the first arg to `env-cmd` to be the relative path to the env file: `env-cmd env_file command carg1 carg2` |
@@ -61,33 +61,35 @@ 'use strict' | ||
function ParseEnvString (envFileString) { | ||
const envs = Object.assign({}, process.env) | ||
while (envFileString.length) { | ||
// The the last index of the line using the newline delimiter | ||
let endOfLineIndex = envFileString.indexOf('\n') | ||
function StripComments (envString) { | ||
const commentsRegex = /[ ]*(#.*$)/gim | ||
return envString.replace(commentsRegex, '') | ||
} | ||
// If no newline, then assume end of file | ||
if (endOfLineIndex === -1) { | ||
endOfLineIndex = envFileString.length | ||
} | ||
function StripEmptyLines (envString) { | ||
const emptyLinesRegex = /(^\n)/gim | ||
return envString.replace(emptyLinesRegex, '') | ||
} | ||
// Get the full line | ||
const line = envFileString.slice(0, endOfLineIndex + 1) | ||
function ParseEnvVars (envString) { | ||
const envParseRegex = /^((.+?)[ =](.*))$/gim | ||
const matches = {} | ||
let match | ||
while ((match = envParseRegex.exec(envString)) !== null) { | ||
// Note: match[1] is the full env=var line | ||
matches[match[2]] = match[3] | ||
} | ||
return matches | ||
} | ||
// Shrink the file by 1 line | ||
envFileString = envFileString.slice(line.length) | ||
function ParseEnvString (envFileString) { | ||
// First thing we do is stripe out all comments | ||
envFileString = StripComments(envFileString) | ||
// Only parse lines that are not empty and don't begin with # | ||
if (line.length > 1 && line[0] !== '#') { | ||
// Parse the line | ||
const equalSign = line.indexOf('=') | ||
// Next we stripe out all the empty lines | ||
envFileString = StripEmptyLines(envFileString) | ||
if (equalSign === -1) { | ||
throw new Error('Error! Malformed line in env file.') | ||
} | ||
// Parse the envs vars out | ||
const envs = ParseEnvVars(envFileString) | ||
// Set then new env var | ||
envs[line.slice(0, equalSign)] = line.slice(equalSign + 1, endOfLineIndex) | ||
} | ||
} | ||
return envs | ||
// Merge the file env vars with the current process env vars (the file vars overwrite process vars) | ||
return Object.assign({}, process.env, envs) | ||
} | ||
@@ -118,3 +120,6 @@ | ||
PrintHelp, | ||
HandleUncaughtExceptions | ||
HandleUncaughtExceptions, | ||
StripComments, | ||
StripEmptyLines, | ||
ParseEnvVars | ||
} |
{ | ||
"name": "env-cmd", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Executes a command using the envs in the provided env file", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -8,3 +8,3 @@ [![Travis](https://img.shields.io/travis/toddbluhm/env-cmd.svg?maxAge=2592000)](https://travis-ci.org/toddbluhm/env-cmd) | ||
# env-cmd | ||
A simple node program for executing commands using an environment from an env file | ||
A simple node program for executing commands using an environment from an env file. | ||
@@ -15,13 +15,12 @@ ## Install | ||
## Usage | ||
**Environment file ``./test/.env`** | ||
**Environment file `./test/.env`** | ||
``` | ||
# This is a comment | ||
ENV1=THANKS | ||
ENV2=FORALL | ||
ENV4=THEFISH | ||
ENV1=THANKS # Yay inline comments support | ||
ENV2=FOR ALL | ||
ENV3 THE FISH # This format is also accepted | ||
``` | ||
*This is the only accepted format for an environment file. If other formats are desired please create an issue* | ||
**Package.json** | ||
```js | ||
```json | ||
{ | ||
@@ -40,2 +39,8 @@ "scripts": { | ||
## Environment File Formats | ||
These are the currently accepted environment file formats. If any other formats are desired please create an issue. | ||
- `key=value` | ||
- `key value` | ||
## Why | ||
@@ -45,6 +50,10 @@ | ||
**Do not commit sensitive env data to a public git repo!** | ||
**Do not commit sensitive environment data to a public git repo!** | ||
## Related Projects | ||
[`cross-env`](https://github.com/kentcdodds/cross-env) - Cross platform setting of environment scripts | ||
## Special Thanks | ||
Special thanks to [cross-env](https://github.com/kentcdodds/cross-env) for inspiration (use's the same `cross-spawn` lib underneath too). | ||
Special thanks to [`cross-env`](https://github.com/kentcdodds/cross-env) for inspiration (use's the same `cross-spawn` lib underneath too). |
7944
104
56