UNPKG

3.87 kBJavaScriptView Raw
1/**
2 * Isomorphic module to work access the environment (query params, env variables).
3 *
4 * @module map
5 */
6
7import * as map from './map.js'
8import * as string from './string.js'
9import * as conditions from './conditions.js'
10import * as storage from './storage.js'
11import * as f from './function.js'
12
13/* c8 ignore next 2 */
14// @ts-ignore
15export 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]'
16
17/* c8 ignore next */
18export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && !isNode
19/* c8 ignore next 3 */
20export const isMac = typeof navigator !== 'undefined'
21 ? /Mac/.test(navigator.platform)
22 : false
23
24/**
25 * @type {Map<string,string>}
26 */
27let params
28const args = []
29
30/* c8 ignore start */
31const computeParams = () => {
32 if (params === undefined) {
33 if (isNode) {
34 params = map.create()
35 const pargs = process.argv
36 let currParamName = null
37 for (let i = 0; i < pargs.length; i++) {
38 const parg = pargs[i]
39 if (parg[0] === '-') {
40 if (currParamName !== null) {
41 params.set(currParamName, '')
42 }
43 currParamName = parg
44 } else {
45 if (currParamName !== null) {
46 params.set(currParamName, parg)
47 currParamName = null
48 } else {
49 args.push(parg)
50 }
51 }
52 }
53 if (currParamName !== null) {
54 params.set(currParamName, '')
55 }
56 // in ReactNative for example this would not be true (unless connected to the Remote Debugger)
57 } else if (typeof location === 'object') {
58 params = map.create(); // eslint-disable-next-line no-undef
59 (location.search || '?').slice(1).split('&').forEach((kv) => {
60 if (kv.length !== 0) {
61 const [key, value] = kv.split('=')
62 params.set(`--${string.fromCamelCase(key, '-')}`, value)
63 params.set(`-${string.fromCamelCase(key, '-')}`, value)
64 }
65 })
66 } else {
67 params = map.create()
68 }
69 }
70 return params
71}
72/* c8 ignore stop */
73
74/**
75 * @param {string} name
76 * @return {boolean}
77 */
78/* c8 ignore next */
79export const hasParam = (name) => computeParams().has(name)
80
81/**
82 * @param {string} name
83 * @param {string} defaultVal
84 * @return {string}
85 */
86/* c8 ignore next 2 */
87export const getParam = (name, defaultVal) =>
88 computeParams().get(name) || defaultVal
89
90/**
91 * @param {string} name
92 * @return {string|null}
93 */
94/* c8 ignore next 4 */
95export const getVariable = (name) =>
96 isNode
97 ? conditions.undefinedToNull(process.env[name.toUpperCase().replaceAll('-', '_')])
98 : conditions.undefinedToNull(storage.varStorage.getItem(name))
99
100/**
101 * @param {string} name
102 * @return {string|null}
103 */
104/* c8 ignore next 2 */
105export const getConf = (name) =>
106 computeParams().get('--' + name) || getVariable(name)
107
108/**
109 * @param {string} name
110 * @return {string}
111 */
112/* c8 ignore next 5 */
113export const ensureConf = (name) => {
114 const c = getConf(name)
115 if (c == null) throw new Error(`Expected configuration "${name.toUpperCase().replaceAll('-', '_')}"`)
116 return c
117}
118
119/**
120 * @param {string} name
121 * @return {boolean}
122 */
123/* c8 ignore next 2 */
124export const hasConf = (name) =>
125 hasParam('--' + name) || getVariable(name) !== null
126
127/* c8 ignore next */
128export const production = hasConf('production')
129
130/* c8 ignore next 2 */
131const forceColor = isNode &&
132 f.isOneOf(process.env.FORCE_COLOR, ['true', '1', '2'])
133
134/* c8 ignore start */
135export const supportsColor = !hasParam('--no-colors') &&
136 (!isNode || process.stdout.isTTY || forceColor) && (
137 !isNode || hasParam('--color') || forceColor ||
138 getVariable('COLORTERM') !== null ||
139 (getVariable('TERM') || '').includes('color')
140)
141/* c8 ignore stop */