Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

env-cmd

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

env-cmd - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

7

CHANGELOG.md
# Changelog
## 2.0.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`
- **Change:** `ParseEnvFile` is now more properly named `ParseEnvString`
- **Feature:** `ParseEnvString` will ignore comment lines (lines starting with `#`)
- **Feature:** `ParseEnvString` will ignore empty lines in env file
- **Bug:** `ParseEnvString` will extract the last line even if no newline (`\n`) exists on it
## 1.0.1

@@ -4,0 +11,0 @@ - Fixed badges

70

lib/index.js

@@ -8,4 +8,17 @@ 'use strict'

function EnvCmd (args) {
// Parse the args from the command line
const parsedArgs = ParseArgs(args)
const env = ParseEnvFile(parsedArgs.envFilePath)
// Attempt to open the provided file
let file
try {
file = fs.readFileSync(parsedArgs.envFilePath, { encoding: 'utf8' })
} catch (e) {
throw new Error(`Error! Could not find or read file at ${parsedArgs.envFilePath}`)
}
// Parse the env file string
const env = ParseEnvString(file)
// Execute the command with the given environment variables
if (parsedArgs.command) {

@@ -23,7 +36,6 @@ const proc = spawn(parsedArgs.command, parsedArgs.commandArgs, {

function ParseArgs (args) {
if (args.length < 3) {
if (args.length < 2) {
throw new Error('Error! Too few arguments passed to env-cmd.')
}
const envFileFlags = /(^\-e$|^\-\-env$)/g
let envFilePath

@@ -35,5 +47,5 @@ let command

// if this is the env file flag the get the file
if (arg.match(envFileFlags)) {
envFilePath = path.resolve(process.cwd(), commandArgs.shift())
// assume the first arg is the env file
if (!envFilePath) {
envFilePath = path.resolve(process.cwd(), arg)
} else {

@@ -45,6 +57,2 @@ command = arg

if (!envFilePath) {
throw new Error('Error! No -e or --env flag passed.')
}
return {

@@ -57,21 +65,31 @@ envFilePath,

function ParseEnvFile (envFilePath) {
let file = fs.readFileSync(envFilePath, { encoding: 'utf8' })
function ParseEnvString (envFileString) {
const envs = Object.assign({}, process.env)
while (file.length) {
while (envFileString.length) {
// The the last index of the line using the newline delimiter
let endOfLineIndex = envFileString.indexOf('\n')
// If no newline, then assume end of file
if (endOfLineIndex === -1) {
endOfLineIndex = envFileString.length
}
// Get the full line
const line = file.slice(0, file.indexOf('\n') + 1)
const line = envFileString.slice(0, endOfLineIndex + 1)
// Shrink the file by 1 line
file = file.slice(line.length)
envFileString = envFileString.slice(line.length)
// Parse the line
const equalSign = line.indexOf('=')
// 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('=')
if (equalSign === -1) {
throw new Error(`Error! Malformed line in ${path.parse(envFilePath).base}.`)
if (equalSign === -1) {
throw new Error('Error! Malformed line in env file.')
}
// Set then new env var
envs[line.slice(0, equalSign)] = line.slice(equalSign + 1, endOfLineIndex)
}
// Set then new env var
envs[line.slice(0, equalSign)] = line.slice(line.indexOf('=') + 1, -1)
}

@@ -83,9 +101,5 @@ return envs

return `
Usage: env-cmd -e [file] command [command options]
Usage: env-cmd env_file command [command options]
A simple application for running a cli application using an env config file
Options:
-e, --env Relative path to the env file
`

@@ -107,5 +121,5 @@ }

ParseArgs,
ParseEnvFile,
ParseEnvString,
PrintHelp,
HandleUncaughtExceptions
}

2

package.json
{
"name": "env-cmd",
"version": "1.0.1",
"version": "2.0.0",
"description": "Executes a command using the envs in the provided env file",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

[![Travis](https://img.shields.io/travis/toddbluhm/env-cmd.svg?maxAge=2592000)](https://travis-ci.org/toddbluhm/env-cmd)
[![Coveralls](https://img.shields.io/coveralls/toddbluhm/env-cmd.svg?maxAge=2592000)](https://coveralls.io/github/toddbluhm/env-cmd)
[![npm](https://img.shields.io/npm/v/env-cmd.svg?maxAge=2592000)](https://www.npmjs.com/package/env-cmd)
[![npm](https://img.shields.io/npm/dm/env-cmd.svg?maxAge=2592000)](https://www.npmjs.com/package/env-cmd)

@@ -15,2 +16,3 @@ [![npm](https://img.shields.io/npm/l/env-cmd.svg?maxAge=2592000)](https://www.npmjs.com/package/env-cmd)

```
# This is a comment
ENV1=THANKS

@@ -26,3 +28,3 @@ ENV2=FORALL

"scripts": {
"test": "env-cmd -e ./test/.env mocha -R spec"
"test": "env-cmd ./test/.env mocha -R spec"
}

@@ -35,3 +37,3 @@ }

```sh
./node_modules/.bin/env-cmd -e ./test/.env node index.js
./node_modules/.bin/env-cmd ./test/.env node index.js
```

@@ -41,8 +43,8 @@

Because sometimes it just too cumbersome passing tons of environment variables to scripts. Its usually just easier to have a file with all the vars in them, especially for development.
Because sometimes its just too cumbersome passing lots of environment variables to scripts. Its usually just easier to have a file with all the vars in them, especially for development and testing.
**Do not commit sensitive data to a public git!**
**Do not commit sensitive env data to a public git repo!**
## Special Thanks
Special thanks to [cross-env](https://github.com/kentcdodds/cross-env) for inspiration (use's the same `cross-spawn` lib underneath).
Special thanks to [cross-env](https://github.com/kentcdodds/cross-env) for inspiration (use's the same `cross-spawn` lib underneath too).

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc