env-cmd/lib/index.js
Todd Bluhm 39616b70b1 Removed -e/--env flags, allow # comment lines in env file, bug fixes
- Removed the -e/--env flags in favor of just requiring the first arg to
env-cmd to be the relative path to the env file
- Comments are now valid in env files using the #
- Empty lines in env file are now ignored
- Fixed bug env file no longer needs to end on a newline
2016-08-17 16:24:01 -05:00

119 lines
2.7 KiB
JavaScript

'use strict'
const spawn = require('cross-spawn').spawn
const path = require('path')
const fs = require('fs')
function EnvCmd (args) {
// 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}`)
}
// Parse the env file string
const env = 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
}
}
function ParseArgs (args) {
if (args.length < 2) {
throw new Error('Error! Too few arguments passed to env-cmd.')
}
let envFilePath
let command
let commandArgs = args.slice()
while (commandArgs.length) {
const arg = commandArgs.shift()
// assume the first arg is the env file
if (!envFilePath) {
envFilePath = path.resolve(process.cwd(), arg)
} else {
command = arg
break
}
}
return {
envFilePath,
command,
commandArgs
}
}
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')
// If no newline, then assume end of file
if (endOfLineIndex === -1) {
endOfLineIndex = envFileString.length
}
// Get the full line
const line = envFileString.slice(0, endOfLineIndex + 1)
// Shrink the file by 1 line
envFileString = envFileString.slice(line.length)
// 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 env file.')
}
// Set then new env var
envs[line.slice(0, equalSign)] = line.slice(equalSign + 1, endOfLineIndex)
}
}
return envs
}
function PrintHelp () {
return `
Usage: env-cmd env_file command [command options]
A simple application for running a cli application using an env config file
`
}
function HandleUncaughtExceptions (e) {
if (e.message.match(/passed/gi)) {
console.log(PrintHelp())
}
console.log(e.message)
process.exit(1)
}
process.on('uncaughtException', HandleUncaughtExceptions)
module.exports = {
EnvCmd,
ParseArgs,
ParseEnvString,
PrintHelp,
HandleUncaughtExceptions
}