env-cmd/src/env-cmd.ts
Todd Bluhm 3534069ece Add --use-shell option
- Added test cases for new option
- Fixed up other test cases
- Fixed code coverage issue
2019-05-04 05:35:26 -05:00

56 lines
1.7 KiB
TypeScript

import { spawn } from './spawn'
import { EnvCmdOptions } from './types'
import { TermSignals } from './signal-termination'
import { parseArgs } from './parse-args'
import { getEnvVars } from './get-env-vars'
/**
* Executes env - cmd using command line arguments
* @export
* @param {string[]} args Command line argument to pass in ['-f', './.env']
* @returns {Promise<{ [key: string]: any }>}
*/
export async function CLI (args: string[]): Promise<{ [key: string]: any }> {
// Parse the args from the command line
const parsedArgs = parseArgs(args)
// Run EnvCmd
return exports.EnvCmd(parsedArgs)
}
/**
* The main env-cmd program. This will spawn a new process and run the given command using
* various environment file solutions.
*
* @export
* @param {EnvCmdOptions} { command, commandArgs, envFile, rc, options }
* @returns {Promise<{ [key: string]: any }>} Returns an object containing [environment variable name]: value
*/
export async function EnvCmd (
{ command, commandArgs, envFile, rc, options }: EnvCmdOptions
): Promise<{ [key: string]: any }> {
options = options || {}
let env = await getEnvVars({ envFile, rc })
// Override the merge order if --no-override flag set
if (options.noOverride) {
env = Object.assign({}, env, process.env)
} else {
// Add in the system environment variables to our environment list
env = Object.assign({}, process.env, env)
}
// Execute the command with the given environment variables
const proc = spawn(command, commandArgs, {
stdio: 'inherit',
shell: options.useShell,
env
})
// Handle any termination signals for parent and child proceses
const signals = new TermSignals()
signals.handleUncaughtExceptions()
signals.handleTermSignals(proc)
return env
}