-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathenv.ts
More file actions
285 lines (251 loc) · 8.08 KB
/
env.ts
File metadata and controls
285 lines (251 loc) · 8.08 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import get from 'lodash.get';
import { TwilioError } from '@twilio/flex-plugins-utils-exception';
declare global {
interface Window {
Twilio?: {
Flex: {
Manager: {
getInstance(): {
configuration: {
logLevel: string;
sdkOptions?: {
chat?: {
region?: string;
};
};
};
};
};
};
};
}
}
export type Region = 'dev' | 'stage';
/* eslint-disable import/no-unused-modules */
export enum Environment {
Production = 'production',
Development = 'development',
Test = 'test',
}
export enum Lifecycle {
Test = 'test',
Build = 'build',
Prebuild = 'prebuild',
Deploy = 'deploy',
Predeploy = 'predeploy',
}
/* c8 ignore next */
export const isNode = (): boolean => typeof process === 'object' && `${process}` === '[object process]';
/* c8 ignore next */
export const isWin32 = (): boolean => isNode() && process.platform === 'win32';
/**
* Internal method for setting process.env
* This is used to bypass terser library that does `process.env.FOO` string replacement
* @param key the key to set
* @param value the value set it to
*/
export const setProcessEnv = <T>(key: string, value: T): void => {
if (isNode()) {
process.env[key] = value as unknown as string;
}
};
/**
* Internal method for getting the process.env
* @param key the key to get
*/
export const getProcessEnv = <T>(key: string): T => process.env[key] as unknown as T;
/**
* Helper method to test whether env variable is defined
* @param key the env to lookup
* @return whether the key is set
*/
const isDefined = (key: string | undefined) => typeof key === 'string' && key !== '';
const setValidJSFile = (source: string) => {
if (!source.endsWith('.js')) {
throw new TwilioError(`${source} is not a valid JS file.`);
}
setProcessEnv('FLEX_UI_SRC', source);
};
/* c8 ignore next */
export const hasHttpProxy = (): boolean => isNode() && isDefined(getProcessEnv('HTTP_PROXY'));
export const getHttpProxy = (): string => getProcessEnv('HTTP_PROXY');
export const setHttpProxy = (host: string): void => setProcessEnv('HTTP_PROXY', host);
export const skipPreflightCheck = (): boolean => getProcessEnv('SKIP_PREFLIGHT_CHECK') === 'true';
export const getAccountSid = (): string | undefined => getProcessEnv('TWILIO_ACCOUNT_SID');
export const getAuthToken = (): string | undefined => getProcessEnv('TWILIO_AUTH_TOKEN');
export const hasHost = (): boolean => isDefined(getProcessEnv('HOST'));
export const getHost = (): string | undefined => getProcessEnv('HOST');
export const hasDomain = (): boolean => isDefined(getProcessEnv('DOMAIN'));
export const getDomain = (): string | undefined => getProcessEnv('DOMAIN');
export const setDomain = (domain: string): void => setProcessEnv('DOMAIN', domain);
export const setHost = (host: string): void => setProcessEnv('HOST', host);
export const hasPort = (): boolean => isDefined(getProcessEnv('PORT'));
export const getPort = (): number => Number(getProcessEnv('PORT'));
export const setPort = (port: number): void => setProcessEnv('PORT', String(port));
export const getFlexUISrc = (): string | undefined => getProcessEnv('FLEX_UI_SRC');
export const setFlexUISrc = (source: string): void => setValidJSFile(source.toString());
export const getNodeEnv = (): string => getProcessEnv('NODE_ENV');
export const setNodeEnv = (_env: Environment): void => setProcessEnv('NODE_ENV', _env);
export const getBabelEnv = (): string => getProcessEnv('BABEL_ENV');
export const setBabelEnv = (_env: Environment): void => setProcessEnv('BABEL_ENV', _env);
export const getLifecycle = (): string => getProcessEnv('npm_lifecycle_event');
export const isLifecycle = (cycle: Lifecycle): boolean => getProcessEnv('npm_lifecycle_event') === cycle;
export const isHTTPS = (): boolean => getProcessEnv('HTTPS') === 'true';
export const setWDSSocketHost = (host: string): void => setProcessEnv('WDS_SOCKET_HOST', host);
export const getWDSSocketHost = (): string | undefined => getProcessEnv('WDS_SOCKET_HOST');
export const setWDSSocketPath = (path: string): void => setProcessEnv('WDS_SOCKET_PATH', path);
export const getWDSSocketPath = (): string | undefined => getProcessEnv('WDS_SOCKET_PATH');
export const setWDSSocketPort = (port: number): void => setProcessEnv('WDS_SOCKET_PORT', port.toString());
export const getWDSSocketPort = (): number => Number(getProcessEnv('WDS_SOCKET_PORT'));
export const getWSSocket = (): Record<string, string | undefined> => ({
host: getProcessEnv('WDS_SOCKET_HOST'),
path: getProcessEnv('WDS_SOCKET_PATH'),
port: getProcessEnv('WDS_SOCKET_PORT'),
});
/* c8 ignore next */
export const isCI = (): boolean => isNode() && getProcessEnv('CI') === 'true';
/**
* Sets the Twilio Profile
* @param profile the profile to set
*/
export const setTwilioProfile = (profile: string): void => setProcessEnv('TWILIO_PROFILE', profile);
/**
* Returns the Twilio Profile
*/
export const getTwilioProfile = (): string | undefined => getProcessEnv('TWILIO_PROFILE');
/**
* Sets the environment to persist the terminal
*/
export const persistTerminal = (): void => setProcessEnv('PERSIST_TERMINAL', 'true');
/**
* Determines if the terminal should be persisted or not
*/
export const isTerminalPersisted = (): boolean => isNode() && getProcessEnv('PERSIST_TERMINAL') === 'true';
/**
* Determines whether script should run in quiet mode
*/
export const isQuiet = (): boolean => isNode() && getProcessEnv('QUIET') === 'true';
/**
* Sets the quiet mode
*/
export const setQuiet = (isQuiet: boolean = true): void => setProcessEnv('QUIET', String(isQuiet));
/**
* Returns true if the caller is the CLI
*/
export const isCLI = (): boolean => getProcessEnv('FLEX_PLUGINS_CLI') === 'true';
/**
* Sets the caller to be the CLI
*/
export const setCLI = (): void => setProcessEnv('FLEX_PLUGINS_CLI', 'true');
/**
* Determines if log level should be trace level
*/
export const isTrace = (): boolean => {
if (isNode()) {
return getProcessEnv('TRACE') === 'true';
}
if (window.Twilio) {
return window.Twilio.Flex.Manager.getInstance().configuration.logLevel === 'trace';
}
return false;
};
/**
* Sets the debug mode
*/
export const setDebug = (isDebug: boolean = true): void => setProcessEnv('DEBUG', String(isDebug));
/**
* Returns true if running in debug verbose mode
*/
export const isDebug = (): boolean => {
if (isTrace()) {
return true;
}
if (isNode()) {
return getProcessEnv('DEBUG') === 'true';
}
if (window.Twilio) {
return window.Twilio.Flex.Manager.getInstance().configuration.logLevel === 'debug';
}
return false;
};
/**
* Sets the region
*/
export const setRegion = (region: Region): void => setProcessEnv('TWILIO_REGION', region);
/**
* Returns the region
*/
/* c8 ignore next */
export const getRegion = (): Region | string => {
if (isNode()) {
return getProcessEnv('TWILIO_REGION') as Region;
}
if (window.Twilio) {
const region = get(window.Twilio.Flex.Manager.getInstance(), 'configuration.sdkOptions.chat.region');
if (region && region.indexOf('stage') !== -1) {
return 'stage';
}
if (region && region.indexOf('dev') !== -1) {
return 'dev';
}
return '';
}
const { href } = window.location;
if (href && href.indexOf('flex.stage.twilio') !== -1) {
return 'stage';
}
if (href && href.indexOf('flex.dev.twilio') !== -1) {
return 'dev';
}
return '';
};
export default {
setProcessEnv,
getProcessEnv,
isNode,
isWin32,
persistTerminal,
hasHttpProxy,
getHttpProxy,
skipPreflightCheck,
isTerminalPersisted,
setTwilioProfile,
getTwilioProfile,
isQuiet,
setQuiet,
isCLI,
setCLI,
isCI,
setDebug,
isDebug,
isTrace,
getAccountSid,
getAuthToken,
getRegion,
setRegion,
hasHost,
getHost,
setHost,
hasDomain,
getDomain,
setDomain,
hasPort,
getPort,
setPort,
getFlexUISrc,
setFlexUISrc,
getNodeEnv,
setNodeEnv,
getBabelEnv,
setBabelEnv,
getLifecycle,
isLifecycle,
isHTTPS,
getWDSSocketHost,
setWDSSocketHost,
getWDSSocketPath,
setWDSSocketPath,
setWDSSocketPort,
getWDSSocketPort,
getWSSocket,
};