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 |
|
12 |
|
13 |
|
14 | export const isNode = typeof process !== 'undefined' && process.release && /node|io\.js/.test(process.release.name)
|
15 |
|
16 | export const isBrowser = typeof window !== 'undefined' && !isNode
|
17 |
|
18 | export const isMac = typeof navigator !== 'undefined' ? /Mac/.test(navigator.platform) : false
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | let params
|
24 | const args = []
|
25 |
|
26 |
|
27 | const computeParams = () => {
|
28 | if (params === undefined) {
|
29 | if (isNode) {
|
30 | params = map.create()
|
31 | const pargs = process.argv
|
32 | let currParamName = null
|
33 |
|
34 | for (let i = 0; i < pargs.length; i++) {
|
35 | const parg = pargs[i]
|
36 | if (parg[0] === '-') {
|
37 | if (currParamName !== null) {
|
38 | params.set(currParamName, '')
|
39 | }
|
40 | currParamName = parg
|
41 | } else {
|
42 | if (currParamName !== null) {
|
43 | params.set(currParamName, parg)
|
44 | currParamName = null
|
45 | } else {
|
46 | args.push(parg)
|
47 | }
|
48 | }
|
49 | }
|
50 | if (currParamName !== null) {
|
51 | params.set(currParamName, '')
|
52 | }
|
53 |
|
54 | } else if (typeof location === 'object') {
|
55 | params = map.create()
|
56 |
|
57 | ;(location.search || '?').slice(1).split('&').forEach(kv => {
|
58 | if (kv.length !== 0) {
|
59 | const [key, value] = kv.split('=')
|
60 | params.set(`--${string.fromCamelCase(key, '-')}`, value)
|
61 | params.set(`-${string.fromCamelCase(key, '-')}`, value)
|
62 | }
|
63 | })
|
64 | } else {
|
65 | params = map.create()
|
66 | }
|
67 | }
|
68 | return params
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | export const hasParam = name => computeParams().has(name)
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | export const getParam = (name, defaultVal) => computeParams().get(name) || defaultVal
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | export const getVariable = name => isNode ? conditions.undefinedToNull(process.env[name.toUpperCase()]) : conditions.undefinedToNull(storage.varStorage.getItem(name))
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | export const getConf = name => computeParams().get('--' + name) || getVariable(name)
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | export const hasConf = name => hasParam('--' + name) || getVariable(name) !== null
|
106 |
|
107 |
|
108 | export const production = hasConf('production')
|