-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathvalidateSwaCliIsLatest.ts
More file actions
78 lines (68 loc) · 3.48 KB
/
validateSwaCliIsLatest.ts
File metadata and controls
78 lines (68 loc) · 3.48 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { callWithTelemetryAndErrorHandling, DialogResponses, IActionContext, openUrl } from '@microsoft/vscode-azext-utils';
import * as semver from 'semver';
import * as vscode from 'vscode';
import { installSwaCliUrl } from '../../constants';
import { cpUtils } from '../../utils/cpUtils';
import { localize } from '../../utils/localize';
import { getWorkspaceSetting, updateGlobalSetting } from '../../utils/settingsUtils';
import { getInstalledSwaCliVersion } from './getInstalledSwaCliVersion';
import { getNewestSwaCliVersion } from './getNewestSwaCliVersion';
import { installOrUpdateSwaCli } from './installOrUpdateSwaCli';
import { verifyHasNpm } from './verifyHasNpm';
export async function validateStaticWebAppsCliIsLatest(): Promise<void> {
await callWithTelemetryAndErrorHandling('staticWebApps.validateStaticWebAppsCliIsLatest', async (context: IActionContext) => {
context.errorHandling.suppressDisplay = true;
context.telemetry.properties.isActivationEvent = 'true';
const showSwaCliWarningKey: string = 'showStaticWebAppsCliWarning';
const showSwaCliWarning: boolean = !!getWorkspaceSetting<boolean>(showSwaCliWarningKey);
if (!await verifyHasNpm(context)) {
return;
}
context.telemetry.properties.hasNpm = 'true';
if (showSwaCliWarning) {
const installedVersion: string | null = await getInstalledSwaCliVersion();
if (!installedVersion) {
return;
}
context.telemetry.properties.localVersion = installedVersion;
const newestVersion: string | undefined = await getNewestSwaCliVersion(context);
if (!newestVersion) {
return;
}
if (semver.gt(newestVersion, installedVersion)) {
context.telemetry.properties.outOfDateSwaCli = 'true';
const message: string = localize(
'outdatedSwaCli',
'Update the Azure Static Web Apps CLI ({0}) to the latest ({1}) for the best experience.',
installedVersion,
newestVersion
);
const update: vscode.MessageItem = { title: localize('update', 'Update') };
let result: vscode.MessageItem;
do {
result = await context.ui.showWarningMessage(message, update, DialogResponses.learnMore, DialogResponses.dontWarnAgain);
if (result === DialogResponses.learnMore) {
await openUrl(installSwaCliUrl);
} else if (result === update) {
await installOrUpdateSwaCli(context);
} else if (result === DialogResponses.dontWarnAgain) {
await updateGlobalSetting(showSwaCliWarningKey, false);
}
}
while (result === DialogResponses.learnMore);
}
}
});
}
export async function hasNpm(): Promise<boolean> {
try {
await cpUtils.executeCommand(undefined, undefined, 'npm', '--version');
return true;
} catch (e) {
return false;
}
}