MCPcopy
hub / github.com/motdotla/dotenv / configDotenv

Function configDotenv

lib/main.js:240–317  ·  view source on GitHub ↗
(options)

Source from the content-addressed store, hash-verified

238}
239
240function configDotenv (options) {
241 const dotenvPath = path.resolve(process.cwd(), '.env')
242 let encoding = 'utf8'
243 let processEnv = process.env
244 if (options && options.processEnv != null) {
245 processEnv = options.processEnv
246 }
247 let debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || (options && options.debug))
248 let quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || (options && options.quiet))
249
250 if (options && options.encoding) {
251 encoding = options.encoding
252 } else {
253 if (debug) {
254 _debug('no encoding is specified (UTF-8 is used by default)')
255 }
256 }
257
258 let optionPaths = [dotenvPath] // default, look for .env
259 if (options && options.path) {
260 if (!Array.isArray(options.path)) {
261 optionPaths = [_resolveHome(options.path)]
262 } else {
263 optionPaths = [] // reset default
264 for (const filepath of options.path) {
265 optionPaths.push(_resolveHome(filepath))
266 }
267 }
268 }
269
270 // Build the parsed data in a temporary object (because we need to return it). Once we have the final
271 // parsed data, we will combine it with process.env (or options.processEnv if provided).
272 let lastError
273 const parsedAll = {}
274 for (const path of optionPaths) {
275 try {
276 // Specifying an encoding returns a string instead of a buffer
277 const parsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))
278
279 DotenvModule.populate(parsedAll, parsed, options)
280 } catch (e) {
281 if (debug) {
282 _debug(`failed to load ${path} ${e.message}`)
283 }
284 lastError = e
285 }
286 }
287
288 const populated = DotenvModule.populate(processEnv, parsedAll, options)
289
290 // handle user settings DOTENV_CONFIG_ options inside .env file(s)
291 debug = parseBoolean(processEnv.DOTENV_CONFIG_DEBUG || debug)
292 quiet = parseBoolean(processEnv.DOTENV_CONFIG_QUIET || quiet)
293
294 if (debug || !quiet) {
295 const keysCount = Object.keys(populated).length
296 const shortPaths = []
297 for (const filePath of optionPaths) {

Callers

nothing calls this directly

Calls 6

parseBooleanFunction · 0.85
_debugFunction · 0.85
_resolveHomeFunction · 0.85
_logFunction · 0.85
dimFunction · 0.85
_getRandomTipFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…