-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathlocal.ts
More file actions
285 lines (260 loc) · 9.13 KB
/
local.ts
File metadata and controls
285 lines (260 loc) · 9.13 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 {isTruthy} from './utilities.js'
import {getCIMetadata, isSet, Metadata} from '../../../private/node/context/utilities.js'
import {defaultThemeKitAccessDomain, environmentVariables, pathConstants} from '../../../private/node/constants.js'
import {fileExists} from '../fs.js'
import {exec} from '../system.js'
import isInteractive from 'is-interactive'
import macaddress from 'macaddress'
import {homedir} from 'os'
/**
* It returns true if the terminal is interactive.
*
* @returns True if the terminal is interactive.
*/
export function isTerminalInteractive(): boolean {
return isInteractive()
}
/**
* Returns the path to the user's home directory.
*
* @returns The path to the user's home directory.
*/
export function homeDirectory(): string {
return homedir()
}
/**
* Returns true if the CLI is running in debug mode.
*
* @param env - The environment variables from the environment of the current process.
* @returns True if SHOPIFY_ENV is development.
*/
export function isDevelopment(env = process.env): boolean {
return env[environmentVariables.env] === 'development'
}
/**
* Returns true if the CLI is running in verbose mode.
*
* @param env - The environment variables from the environment of the current process.
* @returns True if SHOPIFY_FLAG_VERBOSE is truthy or the flag --verbose has been passed.
*/
export function isVerbose(env = process.env): boolean {
return isTruthy(env[environmentVariables.verbose]) || process.argv.includes('--verbose')
}
/**
* Returns true if the environment in which the CLI is running is either
* a local environment (where dev is present).
*
* @param env - The environment variables from the environment of the current process.
* @returns True if the CLI is used in a Shopify environment.
*/
export async function isShopify(env = process.env): Promise<boolean> {
if (Object.prototype.hasOwnProperty.call(env, environmentVariables.runAsUser)) {
return !isTruthy(env[environmentVariables.runAsUser])
}
const devInstalled = await fileExists(pathConstants.executables.dev)
return devInstalled
}
/**
* This variable is used when running unit tests to indicate that the CLI's business logic
* is run as a subject of a unit test. We can use this variable to disable output through
* the standard streams.
*
* @param env - The environment variables from the environment of the current process.
* @returns True if the SHOPIFY_UNIT_TEST environment variable is truthy.
*/
export function isUnitTest(env = process.env): boolean {
return isTruthy(env[environmentVariables.unitTest])
}
/**
* Returns true if reporting analytics is enabled.
*
* @param env - The environment variables from the environment of the current process.
* @returns True unless SHOPIFY_CLI_NO_ANALYTICS is truthy or debug mode is enabled.
*/
export function analyticsDisabled(env = process.env): boolean {
return isTruthy(env[environmentVariables.noAnalytics]) || isDevelopment(env)
}
/**
* Returns true if reporting analytics should always happen, regardless of DEBUG mode etc.
*
* @param env - The environment variables from the environment of the current process.
* @returns True if SHOPIFY_CLI_ALWAYS_LOG_ANALYTICS is truthy.
*/
export function alwaysLogAnalytics(env = process.env): boolean {
return isTruthy(env[environmentVariables.alwaysLogAnalytics])
}
/**
* Returns true if reporting metrics should always happen, regardless of DEBUG mode etc.
*
* @param env - The environment variables from the environment of the current process.
* @returns True if SHOPIFY_CLI_ALWAYS_LOG_METRICS is truthy.
*/
export function alwaysLogMetrics(env = process.env): boolean {
return isTruthy(env[environmentVariables.alwaysLogMetrics])
}
/**
* Returns true if the CLI User is 1P.
*
* @param env - The environment variables from the environment of the current process.
* @returns True if SHOPIFY_CLI_1P is truthy.
*/
export function firstPartyDev(env = process.env): boolean {
return isTruthy(env[environmentVariables.firstPartyDev])
}
/**
* Return gitpodURL if we are running in gitpod.
* Https://www.gitpod.io/docs/environment-variables#default-environment-variables.
*
* @param env - The environment variables from the environment of the current process.
* @returns The gitpod URL.
*/
export function gitpodURL(env = process.env): string | undefined {
return env[environmentVariables.gitpod]
}
/**
* Return codespaceURL if we are running in codespaces.
* Https://docs.github.com/en/codespaces/developing-in-codespaces/default-environment-variables-for-your-codespace#list-of-default-environment-variables.
*
* @param env - The environment variables from the environment of the current process.
* @returns The codespace URL.
*/
export function codespaceURL(env = process.env): string | undefined {
return env[environmentVariables.codespaceName]
}
/**
* Return codespacePortForwardingDomain if we are running in codespaces.
* Https://docs.github.com/en/codespaces/developing-in-codespaces/default-environment-variables-for-your-codespace#list-of-default-environment-variables.
*
* @param env - The environment variables from the environment of the current process.
* @returns The codespace port forwarding domain.
*/
export function codespacePortForwardingDomain(env = process.env): string | undefined {
return env[environmentVariables.codespacePortForwardingDomain]
}
/**
* Checks if the CLI is run from a cloud environment.
*
* @param env - Environment variables used when the cli is launched.
* @returns True in case the CLI is run from a cloud environment.
*/
export function isCloudEnvironment(env: NodeJS.ProcessEnv = process.env): boolean {
return cloudEnvironment(env).platform !== 'localhost'
}
/**
* The token used to run a theme command with a custom password.
*
* @param env - Environment variables used when the cli is launched.
* @returns A string with the token.
*/
export function themeToken(env = process.env): string | undefined {
return env[environmentVariables.themeToken]
}
/**
* Returns the cloud environment platform name and if the platform support online IDE in case the CLI is run from one of
* them. Platform name 'localhost' is returned otherwise.
*
* @param env - Environment variables used when the cli is launched.
* @returns Cloud platform information.
*/
export function cloudEnvironment(env: NodeJS.ProcessEnv = process.env): {
platform: 'codespaces' | 'gitpod' | 'cloudShell' | 'localhost'
editor: boolean
} {
if (isSet(env[environmentVariables.codespaces])) {
return {platform: 'codespaces', editor: true}
}
if (isSet(env[environmentVariables.gitpod])) {
return {platform: 'gitpod', editor: true}
}
if (isSet(env[environmentVariables.cloudShell])) {
return {platform: 'cloudShell', editor: true}
}
return {platform: 'localhost', editor: false}
}
/**
* Returns whether the environment has Git available.
*
* @returns A promise that resolves with the value.
*/
export async function hasGit(): Promise<boolean> {
try {
await exec('git', ['--version'])
return true
// eslint-disable-next-line no-catch-all/no-catch-all
} catch {
return false
}
}
/**
* Gets info on the CI platform the CLI is running on, if applicable.
*
* @param env - The environment variables from the environment of the current process.
* @returns The CI platform info.
*/
export function ciPlatform(
env = process.env,
): {isCI: true; name: string; metadata: Metadata} | {isCI: false; name?: undefined; metadata?: undefined} {
if (isTruthy(env.CI)) {
let name = 'unknown'
if (isSet(env.BITBUCKET_BUILD_NUMBER)) {
name = 'bitbucket'
} else if (isTruthy(env.CIRCLECI)) {
name = 'circleci'
} else if (isSet(env.GITHUB_ACTION)) {
name = 'github'
} else if (isTruthy(env.GITLAB_CI)) {
name = 'gitlab'
} else if (isSet(env.BUILDKITE)) {
name = 'buildkite'
}
return {
isCI: true,
name,
metadata: getCIMetadata(name, env),
}
} else if (isTruthy(env.TF_BUILD)) {
return {
isCI: true,
name: 'azure',
metadata: getCIMetadata('azure', env),
}
}
return {
isCI: false,
}
}
/**
* Returns the first mac address found.
*
* @returns Mac address.
*/
export function macAddress(): Promise<string> {
return macaddress.one()
}
/**
* Get the domain for theme kit access.
*
* It can be overridden via the SHOPIFY_CLI_THEME_KIT_ACCESS_DOMAIN environment
* variable.
*
* @param env - The environment variables from the environment of the process.
*
* @returns The domain for theme kit access.
*/
export function getThemeKitAccessDomain(env = process.env): string {
const domain = env[environmentVariables.themeKitAccessDomain]
return isSet(domain) ? domain : defaultThemeKitAccessDomain
}
/**
* Get the domain to send OTEL metrics to.
*
* It can be overridden via the SHOPIFY_CLI_OTEL_EXPORTER_OTLP_ENDPOINT environment variable.
*
* @param env - The environment variables from the environment of the current process.
* @returns The domain to send OTEL metrics to.
*/
export function opentelemetryDomain(env = process.env): string {
const domain = env[environmentVariables.otelURL]
return isSet(domain) ? domain : 'https://otlp-http-production-cli.shopifysvc.com'
}
export type CIMetadata = Metadata