-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathenv-var.js
70 lines (60 loc) · 2.13 KB
/
env-var.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict'
const variable = require('./lib/variable')
const EnvVarError = require('./lib/env-error')
/**
* Returns an "env-var" instance that reads from the given container of values.
* By default, we export an instance that reads from process.env
* @param {Object} container target container to read values from
* @param {Object} extraAccessors additional accessors to attach to the
* resulting object
* @return {Object} a new module instance
*/
const from = (container, extraAccessors, logger) => {
return {
from: from,
/**
* This is the Error class used to generate exceptions. Can be used to identify
* exceptions and handle them appropriately.
*/
EnvVarError: require('./lib/env-error'),
/**
* Returns a variable instance with helper functions, or process.env
* @param {String} variableName Name of the environment variable requested
* @return {Object}
*/
get: function (variableName) {
if (!variableName) {
return container
}
if (arguments.length > 1) {
throw new EnvVarError('It looks like you passed more than one argument to env.get(). Since [email protected] this is no longer supported. To set a default value use env.get(TARGET).default(DEFAULT)')
}
return variable(container, variableName, extraAccessors || {}, logger || function noopLogger () {})
},
/**
* Provides access to the functions that env-var uses to parse
* process.env strings into valid types requested by the API
*/
accessors: require('./lib/accessors/index'),
/**
* Provides a default logger that can be used to print logs.
* This will not print logs in a production environment (checks process.env.NODE_ENV)
*/
logger: require('./lib/logger')(console.log, container.NODE_ENV)
}
}
/**
* Makes a best-effort attempt to load environment variables in
* different environments, e.g create-react-app, vite, Node.js
* @returns Object
*/
function getProcessEnv () {
/* istanbul ignore next */
try {
return process.env
} catch (e) {
return {}
}
}
/* istanbul ignore next */
module.exports = from(getProcessEnv())