diff --git a/src/index.js b/src/index.js index 1086830..0e223d0 100644 --- a/src/index.js +++ b/src/index.js @@ -7,13 +7,17 @@ module.exports = crossEnv const envSetterRegex = /(\w+)=('(.+)'|"(.+)"|(.+))/ function crossEnv(args) { - const [command, commandArgs, env] = getCommandArgsAndEnvVars(args) + const [envSetters, command, commandArgs] = parseCommand(args) if (command) { - const proc = spawn(command, commandArgs, { - stdio: 'inherit', - shell: true, - env, - }) + const proc = spawn( + commandConvert(command), + commandArgs.map(commandConvert), + { + stdio: 'inherit', + shell: true, + env: getEnvVars(envSetters), + }, + ) process.on('SIGTERM', () => proc.kill('SIGTERM')) process.on('SIGINT', () => proc.kill('SIGINT')) process.on('SIGBREAK', () => proc.kill('SIGBREAK')) @@ -24,30 +28,31 @@ function crossEnv(args) { return null } -function getCommandArgsAndEnvVars(args) { - const envVars = getEnvVars() - const commandArgs = args.slice() - const command = getCommand(commandArgs, envVars) - return [commandConvert(command), commandArgs.map(commandConvert), envVars] -} - -function getCommand(commandArgs, envVars) { - while (commandArgs.length) { - const shifted = commandArgs.shift() - const match = envSetterRegex.exec(shifted) +function parseCommand(args) { + const envSetters = {} + let command = null + let commandArgs = [] + for (let i = 0; i < args.length; i++) { + const match = envSetterRegex.exec(args[i]) if (match) { - envVars[match[1]] = varValueConvert(match[3] || match[4] || match[5]) + envSetters[match[1]] = match[3] || match[4] || match[5] } else { - return shifted + // No more env setters, the rest of the line must be the command and args + command = args[i] + commandArgs = args.slice(i + 1) + break } } - return null + return [envSetters, command, commandArgs] } -function getEnvVars() { +function getEnvVars(envSetters) { const envVars = Object.assign({}, process.env) if (process.env.APPDATA) { envVars.APPDATA = process.env.APPDATA } + Object.keys(envSetters).forEach(varName => { + envVars[varName] = varValueConvert(envSetters[varName]) + }) return envVars }