-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
environment.js
152 lines (138 loc) · 4.11 KB
/
environment.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Isomorphic module to work access the environment (query params, env variables).
*
* @module environment
*/
import * as map from './map.js'
import * as string from './string.js'
import * as conditions from './conditions.js'
import * as storage from './storage.js'
import * as f from './function.js'
/* c8 ignore next 2 */
// @ts-ignore
export const isNode = typeof process !== 'undefined' && process.release && /node|io\.js/.test(process.release.name) && Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]'
/* c8 ignore next */
export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && !isNode
/* c8 ignore next 3 */
export const isMac = typeof navigator !== 'undefined'
? /Mac/.test(navigator.platform)
: false
/**
* @type {Map<string,string>}
*/
let params
const args = []
/* c8 ignore start */
const computeParams = () => {
if (params === undefined) {
if (isNode) {
params = map.create()
const pargs = process.argv
let currParamName = null
for (let i = 0; i < pargs.length; i++) {
const parg = pargs[i]
if (parg[0] === '-') {
if (currParamName !== null) {
params.set(currParamName, '')
}
currParamName = parg
} else {
if (currParamName !== null) {
params.set(currParamName, parg)
currParamName = null
} else {
args.push(parg)
}
}
}
if (currParamName !== null) {
params.set(currParamName, '')
}
// in ReactNative for example this would not be true (unless connected to the Remote Debugger)
} else if (typeof location === 'object') {
params = map.create(); // eslint-disable-next-line no-undef
(location.search || '?').slice(1).split('&').forEach((kv) => {
if (kv.length !== 0) {
const [key, value] = kv.split('=')
params.set(`--${string.fromCamelCase(key, '-')}`, value)
params.set(`-${string.fromCamelCase(key, '-')}`, value)
}
})
} else {
params = map.create()
}
}
return params
}
/* c8 ignore stop */
/**
* @param {string} name
* @return {boolean}
*/
/* c8 ignore next */
export const hasParam = (name) => computeParams().has(name)
/**
* @param {string} name
* @param {string} defaultVal
* @return {string}
*/
/* c8 ignore next 2 */
export const getParam = (name, defaultVal) =>
computeParams().get(name) || defaultVal
/**
* @param {string} name
* @return {string|null}
*/
/* c8 ignore next 4 */
export const getVariable = (name) =>
isNode
? conditions.undefinedToNull(process.env[name.toUpperCase().replaceAll('-', '_')])
: conditions.undefinedToNull(storage.varStorage.getItem(name))
/**
* @param {string} name
* @return {string|null}
*/
/* c8 ignore next 2 */
export const getConf = (name) =>
computeParams().get('--' + name) || getVariable(name)
/**
* @param {string} name
* @return {string}
*/
/* c8 ignore next 5 */
export const ensureConf = (name) => {
const c = getConf(name)
if (c == null) throw new Error(`Expected configuration "${name.toUpperCase().replaceAll('-', '_')}"`)
return c
}
/**
* @param {string} name
* @return {boolean}
*/
/* c8 ignore next 2 */
export const hasConf = (name) =>
hasParam('--' + name) || getVariable(name) !== null
/* c8 ignore next */
export const production = hasConf('production')
/* c8 ignore next 2 */
const forceColor = isNode &&
f.isOneOf(process.env.FORCE_COLOR, ['true', '1', '2'])
/* c8 ignore start */
/**
* Color is enabled by default if the terminal supports it.
*
* Explicitly enable color using `--color` parameter
* Disable color using `--no-color` parameter or using `NO_COLOR=1` environment variable.
* `FORCE_COLOR=1` enables color and takes precedence over all.
*/
export const supportsColor = forceColor || (
!hasParam('--no-colors') && // @todo deprecate --no-colors
!hasConf('no-color') &&
(!isNode || process.stdout.isTTY) && (
!isNode ||
hasParam('--color') ||
getVariable('COLORTERM') !== null ||
(getVariable('TERM') || '').includes('color')
)
)
/* c8 ignore stop */