Socket
Socket
Sign inDemoInstall

env-cmd

Package Overview
Dependencies
8
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.2.0 to 3.0.0

5

CHANGELOG.md
# Changelog
## 3.0.0
- **Feature**: Added ability to use an `.env-cmdrc` file to hold multiple configs
- **Feature**: Added ability to pass in a regular `.js` file exporting an object for your env file (special thanks to Jon Scheiding!)
- **Change**: Updated core `cross-spawn` lib to 5.0.1
## 2.2.0

@@ -4,0 +9,0 @@ - **Feature**: Added support for .json env files (special thanks to Eric Lanehart!)

95

lib/index.js

@@ -6,32 +6,22 @@ 'use strict'

const fs = require('fs')
const rcFileLocation = path.join(process.cwd(), '.env-cmdrc')
function EnvCmd (args) {
// Parse the args from the command line
// First Parse the args from the command line
const parsedArgs = ParseArgs(args)
// 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}`)
}
// If a .rc file was found then use that
const env = fs.existsSync(rcFileLocation) ? UseRCFile(parsedArgs) : UseCmdLine(parsedArgs)
// Parse the env file string
const env = path.extname(parsedArgs.envFilePath).toLowerCase() === '.json'
? Object.assign({}, process.env, require(parsedArgs.envFilePath))
: ParseEnvString(file)
// Execute the command with the given environment variables
if (parsedArgs.command) {
const proc = spawn(parsedArgs.command, parsedArgs.commandArgs, {
stdio: 'inherit',
env
})
process.on('SIGTERM', () => proc.kill('SIGTERM'))
proc.on('exit', process.exit)
return proc
}
const proc = spawn(parsedArgs.command, parsedArgs.commandArgs, {
stdio: 'inherit',
env
})
process.on('SIGTERM', proc.kill.bind(proc, 'SIGTERM'))
proc.on('exit', process.exit)
return proc
}
// Parses the arguments passed into the cli
function ParseArgs (args) {

@@ -42,3 +32,3 @@ if (args.length < 2) {

let envFilePath
let envFile
let command

@@ -49,5 +39,5 @@ let commandArgs = args.slice()

// assume the first arg is the env file
if (!envFilePath) {
envFilePath = path.resolve(process.cwd(), arg)
// assume the first arg is the env file (or if using .rc the environment name)
if (!envFile) {
envFile = arg
} else {

@@ -60,3 +50,3 @@ command = arg

return {
envFilePath,
envFile,
command,

@@ -67,2 +57,3 @@ commandArgs

// Strips out comments from env file string
function StripComments (envString) {

@@ -73,2 +64,3 @@ const commentsRegex = /[ ]*(#.*$)/gim

// Strips out newlines from env file string
function StripEmptyLines (envString) {

@@ -79,2 +71,3 @@ const emptyLinesRegex = /(^\n)/gim

// Parse out all env vars from an env file string
function ParseEnvVars (envString) {

@@ -91,2 +84,3 @@ const envParseRegex = /^((.+?)[ =](.*))$/gim

// Parse out all env vars from a given env file string and return an object
function ParseEnvString (envFileString) {

@@ -106,7 +100,45 @@ // First thing we do is stripe out all comments

// Reads and parses the .env-cmdrc file
function ParseRCFile (fileData) {
return JSON.parse(fileData)
}
// Uses the rc file to get env vars
function UseRCFile (parsedArgs) {
const fileData = fs.readFileSync(rcFileLocation, { encoding: 'utf8' })
const parsedData = ParseRCFile(fileData)
return parsedData[parsedArgs.envFile]
}
// Uses the cli passed env file to get env vars
function UseCmdLine (parsedArgs) {
const envFilePath = path.join(process.cwd(), parsedArgs.envFile)
// Attempt to open the provided file
let file
try {
file = fs.readFileSync(envFilePath, { encoding: 'utf8' })
} catch (e) {
throw new Error(`Error! Could not find or read file at ${envFilePath}`)
}
const ext = path.extname(envFilePath).toLowerCase()
// Parse the env file string using the correct parser
const env = ext === '.json' || ext === '.js'
? Object.assign({}, process.env, require(envFilePath))
: ParseEnvString(file)
return env
}
// Prints out some minor help text
function PrintHelp () {
return `
Usage: env-cmd env_file command [command options]
Usage: env-cmd [env_file | env_name] command [command options]
A simple application for running a cli application using an env config file
A simple utility for running a cli application using an env config file.
Also supports using a .env-cmdrc json file in the execution directory to support multiple
environment configs in one file.
`

@@ -133,3 +165,6 @@ }

StripEmptyLines,
ParseEnvVars
ParseEnvVars,
ParseRCFile,
UseRCFile,
UseCmdLine
}
{
"name": "env-cmd",
"version": "2.2.0",
"version": "3.0.0",
"description": "Executes a command using the envs in the provided env file",

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

"test-cover": "istanbul cover node_modules/.bin/_mocha -- -R spec",
"coveralls": "coveralls < coverage/lcov.info"
"test-lint": "standard",
"coveralls": "coveralls < coverage/lcov.info",
"lint": "standard --fix"
},

@@ -29,3 +31,4 @@ "repository": {

"contributors": [
"Eric Lanehart <eric@pushred.co>"
"Eric Lanehart <eric@pushred.co>",
"Jon Scheiding <jonscheiding@gmail.com>"
],

@@ -38,3 +41,3 @@ "license": "MIT",

"dependencies": {
"cross-spawn": "^4.0.0"
"cross-spawn": "^5.0.1"
},

@@ -47,4 +50,5 @@ "devDependencies": {

"proxyquire": "^1.7.10",
"sinon": "^1.17.5"
"sinon": "^1.17.5",
"standard": "^8.6.0"
}
}

@@ -1,6 +0,7 @@

[![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)
[![Travis](https://img.shields.io/travis/toddbluhm/env-cmd.svg)](https://travis-ci.org/toddbluhm/env-cmd)
[![Coveralls](https://img.shields.io/coveralls/toddbluhm/env-cmd.svg)](https://coveralls.io/github/toddbluhm/env-cmd)
[![npm](https://img.shields.io/npm/v/env-cmd.svg?maxAge=86400)](https://www.npmjs.com/package/env-cmd)
[![npm](https://img.shields.io/npm/dm/env-cmd.svg?maxAge=86400)](https://www.npmjs.com/package/env-cmd)
[![npm](https://img.shields.io/npm/l/env-cmd.svg?maxAge=2592000)](https://www.npmjs.com/package/env-cmd)
[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)

@@ -36,3 +37,22 @@ # env-cmd

```
or
**.env-cmdrc file `.env-cmdrc`**
```json
{
"development": {
"ENV1": "Thanks",
"ENV2": "For All"
},
"production": {
"ENV1": "The Fish"
}
}
```
```sh
./node_modules/.bin/env-cmd production node index.js
```
## Environment File Formats

@@ -44,2 +64,4 @@

- Key/value pairs as JSON
- JavaScript file exporting an object
- `.env-cmdrc` file (as valid json) in execution directory

@@ -63,1 +85,9 @@ ## Why

- Eric Lanehart
- Jon Scheiding
## Contributing Guide
I welcome all pull requests. Please make sure you add appropriate test cases for any features added. Before opening a PR please make sure to run the following scripts:
- `npm run lint` checks for code errors and formats according to [js-standard](https://github.com/feross/standard)
- `npm test` make sure all tests pass
- `npm run test-cover` make sure the coverage has not decreased from current master
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc