1"use strict"; 2 3Object.defineProperty(exports, "__esModule", { 4 value: true 5}); 6exports.createControlledEnvironment = createControlledEnvironment; 7exports.getFiles = getFiles; 8function _chalk() { 9 const data = _interopRequireDefault(require("chalk")); 10 _chalk = function () { 11 return data; 12 }; 13 return data; 14} 15function dotenv() { 16 const data = _interopRequireWildcard(require("dotenv")); 17 dotenv = function () { 18 return data; 19 }; 20 return data; 21} 22function _dotenvExpand() { 23 const data = require("dotenv-expand"); 24 _dotenvExpand = function () { 25 return data; 26 }; 27 return data; 28} 29function fs() { 30 const data = _interopRequireWildcard(require("fs")); 31 fs = function () { 32 return data; 33 }; 34 return data; 35} 36function path() { 37 const data = _interopRequireWildcard(require("path")); 38 path = function () { 39 return data; 40 }; 41 return data; 42} 43function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } 44function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 45function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 46/** 47 * Copyright © 2023 650 Industries. 48 * 49 * This source code is licensed under the MIT license found in the 50 * LICENSE file in the root directory of this source tree. 51 */ 52 53const debug = require('debug')('expo:env'); 54function createControlledEnvironment() { 55 const IS_DEBUG = require('debug').enabled('expo:env'); 56 let userDefinedEnvironment = undefined; 57 let memoEnvironment = undefined; 58 function _getForce(projectRoot) { 59 if (!userDefinedEnvironment) { 60 userDefinedEnvironment = { 61 ...process.env 62 }; 63 } 64 65 // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use 66 const dotenvFiles = getFiles(process.env.NODE_ENV); 67 const loadedEnvFiles = []; 68 const parsed = {}; 69 70 // Load environment variables from .env* files. Suppress warnings using silent 71 // if this file is missing. dotenv will never modify any environment variables 72 // that have already been set. Variable expansion is supported in .env files. 73 // https://github.com/motdotla/dotenv 74 // https://github.com/motdotla/dotenv-expand 75 dotenvFiles.forEach(dotenvFile => { 76 const absoluteDotenvFile = path().resolve(projectRoot, dotenvFile); 77 if (!fs().existsSync(absoluteDotenvFile)) { 78 return; 79 } 80 try { 81 const results = (0, _dotenvExpand().expand)(dotenv().config({ 82 debug: IS_DEBUG, 83 path: absoluteDotenvFile, 84 // We will handle overriding ourselves to allow for HMR. 85 override: true 86 })); 87 if (results.parsed) { 88 loadedEnvFiles.push(absoluteDotenvFile); 89 debug(`Loaded environment variables from: ${absoluteDotenvFile}`); 90 for (const key of Object.keys(results.parsed || {})) { 91 var _userDefinedEnvironme; 92 if (typeof parsed[key] === 'undefined' && 93 // Custom override logic to prevent overriding variables that 94 // were set before the CLI process began. 95 typeof ((_userDefinedEnvironme = userDefinedEnvironment) === null || _userDefinedEnvironme === void 0 ? void 0 : _userDefinedEnvironme[key]) === 'undefined') { 96 parsed[key] = results.parsed[key]; 97 } 98 } 99 } else { 100 debug(`Failed to load environment variables from: ${absoluteDotenvFile}`); 101 } 102 } catch (error) { 103 if (error instanceof Error) { 104 console.error(`Failed to load environment variables from ${absoluteDotenvFile}: ${error.message}`); 105 } else { 106 throw error; 107 } 108 } 109 }); 110 if (!loadedEnvFiles.length) { 111 debug(`No environment variables loaded from .env files.`); 112 } 113 return parsed; 114 } 115 116 /** Get the environment variables without mutating the environment. This returns memoized values unless the `force` property is provided. */ 117 function get(projectRoot, { 118 force 119 } = {}) { 120 if (!force && memoEnvironment) { 121 return memoEnvironment; 122 } 123 memoEnvironment = _getForce(projectRoot); 124 return memoEnvironment; 125 } 126 127 /** Load environment variables from .env files and mutate the current `process.env` with the results. */ 128 function load(projectRoot, { 129 force 130 } = {}) { 131 const env = get(projectRoot, { 132 force 133 }); 134 process.env = { 135 ...process.env, 136 ...env 137 }; 138 return process.env; 139 } 140 return { 141 load, 142 get, 143 _getForce 144 }; 145} 146function getFiles(mode) { 147 if (!mode) { 148 console.error(_chalk().default.red('The NODE_ENV environment variable is required but was not specified. Ensure the project is bundled with Expo CLI or NODE_ENV is set.')); 149 console.error(_chalk().default.red('Proceeding without mode-specific .env')); 150 } 151 if (mode && !['development', 'test', 'production'].includes(mode)) { 152 throw new Error(`Environment variable "NODE_ENV=${mode}" is invalid. Valid values are "development", "test", and "production`); 153 } 154 if (!mode) { 155 // Support environments that don't respect NODE_ENV 156 return [`.env.local`, '.env']; 157 } 158 // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use 159 const dotenvFiles = [`.env.${mode}.local`, 160 // Don't include `.env.local` for `test` environment 161 // since normally you expect tests to produce the same 162 // results for everyone 163 mode !== 'test' && `.env.local`, `.env.${mode}`, '.env'].filter(Boolean); 164 return dotenvFiles; 165} 166//# sourceMappingURL=env.js.map