@expo/env
Advanced tools
Comparing version 0.0.0-canary-20231122-1af9191 to 0.0.0-canary-20231123-1b19f96
@@ -66,3 +66,2 @@ "use strict"; | ||
function createControlledEnvironment() { | ||
const IS_DEBUG = require('debug').enabled('expo:env'); | ||
let userDefinedEnvironment = undefined; | ||
@@ -86,11 +85,15 @@ let memo = undefined; | ||
const dotenvFiles = getFiles(process.env.NODE_ENV, options); | ||
const loadedEnvFiles = []; | ||
const parsed = {}; | ||
// Load environment variables from .env* files. Suppress warnings using silent | ||
// if this file is missing. dotenv will never modify any environment variables | ||
// that have already been set. Variable expansion is supported in .env files. | ||
// if this file is missing. Dotenv will only parse the environment variables, | ||
// `@expo/env` will set the resulting variables to the current process. | ||
// Variable expansion is supported in .env files, and executed as final step. | ||
// https://github.com/motdotla/dotenv | ||
// https://github.com/motdotla/dotenv-expand | ||
dotenvFiles.forEach(dotenvFile => { | ||
const parsedEnv = {}; | ||
const loadedEnvFiles = []; | ||
// Iterate over each dotenv file in lowest prio to highest prio order. | ||
// This step won't write to the process.env, but will overwrite the parsed envs. | ||
dotenvFiles.reverse().forEach(dotenvFile => { | ||
const absoluteDotenvFile = path().resolve(projectRoot, dotenvFile); | ||
@@ -101,22 +104,19 @@ if (!fs().existsSync(absoluteDotenvFile)) { | ||
try { | ||
const results = (0, _dotenvExpand().expand)(dotenv().config({ | ||
debug: IS_DEBUG, | ||
path: absoluteDotenvFile, | ||
// We will handle overriding ourselves to allow for HMR. | ||
override: true | ||
})); | ||
if (results.parsed) { | ||
const result = dotenv().parse(fs().readFileSync(absoluteDotenvFile, 'utf-8')); | ||
if (!result) { | ||
debug(`Failed to load environment variables from: ${absoluteDotenvFile}%s`); | ||
} else { | ||
loadedEnvFiles.push(absoluteDotenvFile); | ||
debug(`Loaded environment variables from: ${absoluteDotenvFile}`); | ||
for (const key of Object.keys(results.parsed || {})) { | ||
for (const key of Object.keys(result)) { | ||
var _userDefinedEnvironme; | ||
if (typeof parsed[key] === 'undefined' && | ||
// Custom override logic to prevent overriding variables that | ||
// were set before the CLI process began. | ||
typeof ((_userDefinedEnvironme = userDefinedEnvironment) === null || _userDefinedEnvironme === void 0 ? void 0 : _userDefinedEnvironme[key]) === 'undefined') { | ||
parsed[key] = results.parsed[key]; | ||
if (typeof ((_userDefinedEnvironme = userDefinedEnvironment) === null || _userDefinedEnvironme === void 0 ? void 0 : _userDefinedEnvironme[key]) !== 'undefined') { | ||
debug(`"${key}" is already defined and IS NOT overwritten by: ${absoluteDotenvFile}`); | ||
} else { | ||
if (typeof parsedEnv[key] !== 'undefined') { | ||
debug(`"${key}" is already defined and overwritten by: ${absoluteDotenvFile}`); | ||
} | ||
parsedEnv[key] = result[key]; | ||
} | ||
} | ||
} else { | ||
debug(`Failed to load environment variables from: ${absoluteDotenvFile}`); | ||
} | ||
@@ -135,7 +135,33 @@ } catch (error) { | ||
return { | ||
env: parsed, | ||
files: loadedEnvFiles | ||
env: _expandEnv(parsedEnv), | ||
files: loadedEnvFiles.reverse() | ||
}; | ||
} | ||
/** Expand environment variables based on the current and parsed envs */ | ||
function _expandEnv(parsedEnv) { | ||
const expandedEnv = {}; | ||
// When not ignoring `process.env`, values from the parsed env are overwritten by the current env if defined. | ||
// We handle this ourselves, expansion should always use the current state of "current + parsed env". | ||
const allExpandedEnv = (0, _dotenvExpand().expand)({ | ||
parsed: { | ||
...process.env, | ||
...parsedEnv | ||
}, | ||
ignoreProcessEnv: true | ||
}); | ||
if (allExpandedEnv.error) { | ||
console.error(`Failed to expand environment variables, using non-expanded environment variables: ${allExpandedEnv.error}`); | ||
return parsedEnv; | ||
} | ||
for (const key of Object.keys(parsedEnv)) { | ||
var _allExpandedEnv$parse; | ||
if ((_allExpandedEnv$parse = allExpandedEnv.parsed) !== null && _allExpandedEnv$parse !== void 0 && _allExpandedEnv$parse[key]) { | ||
expandedEnv[key] = allExpandedEnv.parsed[key]; | ||
} | ||
} | ||
return expandedEnv; | ||
} | ||
/** Get the environment variables without mutating the environment. This returns memoized values unless the `force` property is provided. */ | ||
@@ -171,6 +197,6 @@ function get(projectRoot, options = {}) { | ||
} | ||
process.env = { | ||
...process.env, | ||
...envInfo.env | ||
}; | ||
for (const key of Object.keys(envInfo.env)) { | ||
// Avoid creating a new object, mutate it instead as this causes problems in Bun | ||
process.env[key] = envInfo.env[key]; | ||
} | ||
return process.env; | ||
@@ -177,0 +203,0 @@ } |
{ | ||
"name": "@expo/env", | ||
"version": "0.0.0-canary-20231122-1af9191", | ||
"version": "0.0.0-canary-20231123-1b19f96", | ||
"description": "hydrate environment variables from .env files into process.env", | ||
@@ -39,3 +39,3 @@ "main": "build/index.js", | ||
"@types/getenv": "^1.0.0", | ||
"expo-module-scripts": "0.0.0-canary-20231122-1af9191" | ||
"expo-module-scripts": "0.0.0-canary-20231123-1b19f96" | ||
}, | ||
@@ -45,3 +45,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "1af91912cdf13b6f7fa0a8207dc9f024132a9a72" | ||
"gitHead": "1b19f96bd0e50f43e4329e388347423e7cce20b6" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29676
303