get-shell-vars
Advanced tools
Comparing version 1.2.1 to 2.0.0
@@ -7,3 +7,4 @@ "use strict"; | ||
_ = require("lodash"), | ||
processWrapper = require("./process-wrapper"); | ||
processWrapper = require("./process-wrapper"), | ||
temp = require("temp"); | ||
@@ -31,14 +32,16 @@ const addEtcPathToPath = (environmentVariables) => { | ||
/** | ||
* Gets all environment variables that the user has in the default terminal. | ||
* @returns {object} Dictionary with key-value pairs of all environment variables. | ||
*/ | ||
const getEnvironmentVariables = () => { | ||
if (processWrapper.getProcessPlatform() === "win32") { | ||
return process.env; | ||
const getEnvironmentVariablesFromNodeProcess = (pathToShell) => { | ||
try { | ||
temp.track(); | ||
const pathToTempFile = temp.path("get-shell-vars-process-env"); | ||
const bashCommandSpawningNode = `${pathToShell} -ilc "node -e 'require(\\"fs\\").writeFileSync(\\"${pathToTempFile}\\", JSON.stringify(process.env))'"`; | ||
childProcess.execSync(bashCommandSpawningNode); | ||
const env = JSON.parse(fs.readFileSync(pathToTempFile)); | ||
return env; | ||
} catch (err) { | ||
console.log("Unable to get environment variables from node, will try another approach."); | ||
} | ||
}; | ||
const shell = process.env && process.env.SHELL && path.basename(process.env.SHELL) || "bash", | ||
pathToShell = process.env && process.env.SHELL || "/bin/bash"; | ||
const getEnvironmentVariablesFromEnvCommand = (pathToShell) => { | ||
try { | ||
@@ -73,3 +76,3 @@ let sourcedEnvironmentVars = childProcess.execSync(`${pathToShell} -ilc env`); | ||
return addEtcPathToPath(environmentVariables); | ||
return environmentVariables; | ||
} | ||
@@ -79,4 +82,18 @@ } catch (err) { | ||
} | ||
} | ||
return addEtcPathToPath(process.env); | ||
/** | ||
* Gets all environment variables that the user has in the default terminal. | ||
* @returns {object} Dictionary with key-value pairs of all environment variables. | ||
*/ | ||
const getEnvironmentVariables = () => { | ||
if (processWrapper.getProcessPlatform() === "win32") { | ||
return process.env; | ||
} | ||
const shell = process.env && process.env.SHELL && path.basename(process.env.SHELL) || "bash", | ||
pathToShell = process.env && process.env.SHELL || "/bin/bash"; | ||
const environmentVariables = getEnvironmentVariablesFromNodeProcess(pathToShell) || getEnvironmentVariablesFromEnvCommand(pathToShell) || process.env; | ||
return addEtcPathToPath(environmentVariables); | ||
}; | ||
@@ -83,0 +100,0 @@ |
{ | ||
"name": "get-shell-vars", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"description": "Code that sources correct profiles, so that you can use all Environment variables declared in them.", | ||
@@ -34,4 +34,5 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"lodash": "4.16.4" | ||
"lodash": "4.16.4", | ||
"temp": "0.8.3" | ||
} | ||
} |
16364
89
2
+ Addedtemp@0.8.3
+ Addedos-tmpdir@1.0.2(transitive)
+ Addedrimraf@2.2.8(transitive)
+ Addedtemp@0.8.3(transitive)