1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import * as map from './map.js'
|
8 | import * as string from './string.js'
|
9 | import * as conditions from './conditions.js'
|
10 | import * as storage from './storage.js'
|
11 | import * as f from './function.js'
|
12 |
|
13 |
|
14 |
|
15 | export const isNode = typeof process !== 'undefined' && process.release &&
|
16 | /node|io\.js/.test(process.release.name)
|
17 |
|
18 | export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && !isNode
|
19 |
|
20 | export const isMac = typeof navigator !== 'undefined'
|
21 | ? /Mac/.test(navigator.platform)
|
22 | : false
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | let params
|
28 | const args = []
|
29 |
|
30 |
|
31 | const 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 |
|
57 | } else if (typeof location === 'object') {
|
58 | params = map.create();
|
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 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | export const hasParam = (name) => computeParams().has(name)
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | export const getParam = (name, defaultVal) =>
|
88 | computeParams().get(name) || defaultVal
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | export const getVariable = (name) =>
|
96 | isNode
|
97 | ? conditions.undefinedToNull(process.env[name.toUpperCase()])
|
98 | : conditions.undefinedToNull(storage.varStorage.getItem(name))
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | export const getConf = (name) =>
|
106 | computeParams().get('--' + name) || getVariable(name)
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | export const hasConf = (name) =>
|
114 | hasParam('--' + name) || getVariable(name) !== null
|
115 |
|
116 |
|
117 | export const production = hasConf('production')
|
118 |
|
119 |
|
120 | const forceColor = isNode &&
|
121 | f.isOneOf(process.env.FORCE_COLOR, ['true', '1', '2'])
|
122 |
|
123 |
|
124 | export const supportsColor = !hasParam('no-colors') &&
|
125 | (!isNode || process.stdout.isTTY || forceColor) && (
|
126 | !isNode || hasParam('color') || forceColor ||
|
127 | getVariable('COLORTERM') !== null ||
|
128 | (getVariable('TERM') || '').includes('color')
|
129 | )
|
130 |
|