Repository navigation
Expand file tree
/
Copy pathazureDevCli.ts
More file actions
242 lines (201 loc) · 8.66 KB
/
Copy pathazureDevCli.ts
File metadata and controls
242 lines (201 loc) · 8.66 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { AzExtErrorButton, IActionContext } from '@microsoft/vscode-azext-utils';
import { composeArgs, StreamSpawnOptions, withArg, withNamedArg } from '@microsoft/vscode-processutils';
import * as path from 'path';
import * as semver from 'semver';
import * as vscode from 'vscode';
import ext from '../ext';
import { execAsync } from './execAsync';
import { AsyncLazy } from './lazy';
import { isWindows } from './osUtils';
import { setVsCodeContext } from './setVsCodeContext';
// Twenty seconds: generous, but not infinite
export const DefaultAzCliInvocationTimeout: number = 20 * 1000;
const AzdLoginCheckCacheLifetime = 15 * 60 * 1000; // 15 minutes
let azdInstallAttempted: boolean = false;
const azdLoginChecker = new AsyncLazy<LoginStatus | undefined>(getAzdLoginStatus, AzdLoginCheckCacheLifetime);
export interface LoginStatus {
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
readonly status: 'success' | 'unauthenticated' | string;
readonly expiresOn?: string;
}
interface VersionInfo {
readonly azd: {
readonly version: string;
readonly commit: string;
};
}
type AzureDevCli = {
invocation: string,
spawnOptions: (cwd?: string) => StreamSpawnOptions,
};
export async function createAzureDevCli(context: IActionContext): Promise<AzureDevCli> {
const loginStatus = await azdLoginChecker.getValue();
if (!loginStatus) {
context.errorHandling.suppressReportIssue = true;
context.errorHandling.buttons = azdNotInstalledUserChoices();
throw new Error(azdNotInstalledMsg());
}
return createCli();
}
export function scheduleAzdVersionCheck(): void {
const oneSecond = 1 * 1000;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const minimumSupportedVersion = semver.coerce('1.8.0')!;
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- `getAzdVersion` will not throw so this is safe
setTimeout(async () => {
const versionResult = await getAzdVersion();
if (versionResult && !semver.gte(versionResult, minimumSupportedVersion)) {
// We won't show a warning if AZD is not installed, but if it is installed and less than the minimum, we will warn
const install: vscode.MessageItem = {
title: vscode.l10n.t('Update'),
};
const later: vscode.MessageItem = {
title: vscode.l10n.t('Later'),
};
const title = vscode.l10n.t(
'The minimum version of the Azure Developer CLI supported by the extension is {0}, but you have {1}. Would you like to update?',
minimumSupportedVersion.version,
versionResult.version
);
void vscode.window.showWarningMessage(title, { modal: false }, install, later).then(async (result) => {
if (result === install) {
await vscode.commands.executeCommand('azure-dev.commands.cli.install', /* shouldPrompt: */ false);
}
});
}
}, oneSecond);
}
export function scheduleAzdSignInCheck(): void {
const oneSecond = 1 * 1000;
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- `getAzdLoginStatus` will not throw so this is safe
setTimeout(async () => {
const result = await azdLoginChecker.getValue();
if (result !== undefined) {
// If we've reached this point, AZD is installed. We can set the VSCode context that the walk-through uses
await setVsCodeContext('hideAzdInstallStep', true);
// If the user is logged in, we can also set the login context
if (result.status === 'success') {
await setVsCodeContext('hideAzdLoginStep', true);
}
}
}, oneSecond);
}
export function scheduleAzdYamlCheck(): void {
const oneSecond = 1 * 1000;
// eslint-disable-next-line @typescript-eslint/no-misused-promises -- This is a safe fire-and-forget
setTimeout(async () => {
// Look for at most one file named azure.yml or azure.yaml, only at the root, to avoid perf issues
// If one exists, the scaffold step will be hidden from the walkthrough
const fileResults = await vscode.workspace.findFiles('azure.{yml,yaml}', undefined, 1);
if (fileResults?.length) {
await setVsCodeContext('hideAzdScaffoldStep', true);
}
}, oneSecond);
}
export function onAzdInstallAttempted(): void {
azdInstallAttempted = true;
// Clear the install+login state so we'll check again at the next command
azdLoginChecker.clear();
}
export function onAzdLoginAttempted(): void {
// Clear the install+login state so we'll check again at the next command
azdLoginChecker.clear();
}
function createCli(): AzureDevCli {
const azDevCliEnv: NodeJS.ProcessEnv = {
// eslint-disable-next-line @typescript-eslint/naming-convention
'AZURE_DEV_USER_AGENT': ext.userAgent
};
if (!vscode.env.isTelemetryEnabled) {
azDevCliEnv.AZURE_DEV_COLLECT_TELEMETRY = "no";
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
let modifiedPath: string = process.env.PATH!;
// On Unix, the CLI is installed to /usr/local/bin, which is always going to be in the PATH
// On Windows, the install location varies but is generally at %LOCALAPPDATA%\Programs\Azure Dev CLI, especially
// when installed the default way, which the extension does.
// To avoid needing to restart VSCode to get the updated PATH, we'll temporarily add the default install location,
// as long as it's Windows, AZURE_DEV_CLI_PATH is unset, "Azure Dev CLI" isn't already in the PATH (somewhere else?),
// and the user did try to install within this session
if (isWindows() && !process.env.AZURE_DEV_CLI_PATH && !/Azure Dev CLI/i.test(modifiedPath) && azdInstallAttempted) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const defaultAzdInstallLocation = path.join(process.env.LOCALAPPDATA!, 'Programs', 'Azure Dev CLI');
modifiedPath += `;${defaultAzdInstallLocation}`;
}
const combinedEnv: NodeJS.ProcessEnv = {
...process.env,
...azDevCliEnv,
PATH: modifiedPath,
};
return {
invocation: getAzDevInvocation(),
spawnOptions: (cwd?: string) => {
return {
timeout: DefaultAzCliInvocationTimeout,
cwd: cwd,
env: combinedEnv,
windowsHide: true
};
}
};
}
function getAzDevInvocation(): string {
return process.env.AZURE_DEV_CLI_PATH || 'azd';
}
async function getAzdVersion(): Promise<semver.SemVer | undefined> {
const cli = createCli();
const args = composeArgs(
withArg('version'),
withNamedArg('--output', 'json'),
)();
try {
const { stdout } = await execAsync(cli.invocation, args, cli.spawnOptions());
const result = JSON.parse(stdout) as VersionInfo;
return semver.coerce(result?.azd.version) ?? undefined;
} catch {
// If AZD is not installed, return `undefined`
return undefined;
}
}
export async function getAzdLoginStatus(): Promise<LoginStatus | undefined> {
const cli = createCli();
const args = composeArgs(
withArg('auth', 'login', '--check-status'),
withNamedArg('--output', 'json'),
)();
try {
const { stdout } = await execAsync(cli.invocation, args, cli.spawnOptions());
const result = JSON.parse(stdout) as LoginStatus;
return result;
} catch {
// If AZD is not installed, return `undefined`
return undefined;
}
}
function azdNotInstalledMsg(): string {
return vscode.l10n.t("Azure Developer CLI is not installed. Would you like to install it? [Learn More](https://aka.ms/azd-install)");
}
function azdNotInstalledUserChoices(): AzExtErrorButton[] {
const choices: AzExtErrorButton[] = [
{
title: vscode.l10n.t("Install"),
callback: async () => {
await vscode.commands.executeCommand("azure-dev.commands.cli.install", /* shouldPrompt: */ false);
}
},
{
title: vscode.l10n.t("Later"),
callback: () => { return Promise.resolve(); /* no-op */ }
}
];
return choices;
}
// isAzdCommand returns true if this is the command to run azd.
export function isAzdCommand(command: string | undefined): boolean {
if (!command) {
return false;
}
return command === getAzDevInvocation() || command.startsWith(`${getAzDevInvocation()} `);
}