Skip to content

Commit b991bd4

Browse files
committed
Support Dev Proxy install from package managers. Closes #82
Closes #82
1 parent 9541ea3 commit b991bd4

File tree

6 files changed

+77
-21
lines changed

6 files changed

+77
-21
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.4.0] - 2024-05-30
9+
10+
### Changed:
11+
12+
- Notification: Support for installing Dev Proxy via package manager when not installed
13+
814
## [0.3.1] - 2024-05-22
915

10-
### Fixed
16+
### Fixed:
1117

1218
- Fixed issue where `configSection` diagnostic check required `RateLimitingPlugin` to have a `configSection`
1319

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "dev-proxy-toolkit",
33
"displayName": "Dev Proxy Toolkit",
44
"description": "Makes it easy to create and update Dev Proxy configuration files.",
5-
"version": "0.3.1",
5+
"version": "0.4.0",
66
"publisher": "garrytrinder",
77
"engines": {
88
"vscode": "^1.85.0"

src/commands.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
11
import * as vscode from 'vscode';
22
import { pluginDocs } from './constants';
3+
import { VersionPreference } from './enums';
4+
import { executeCommand } from './helpers';
35

4-
export const registerCommands = (context: vscode.ExtensionContext) => {
6+
export const registerCommands = (context: vscode.ExtensionContext, configuration: vscode.WorkspaceConfiguration) => {
57
context.subscriptions.push(
68
vscode.commands.registerCommand('dev-proxy-toolkit.install', async (platform: NodeJS.Platform) => {
7-
const url = `https://aka.ms/devproxy/install/${platform === 'darwin' ? 'macos' : 'windows'}`;
8-
vscode.env.openExternal(vscode.Uri.parse(url));
9+
const versionPreference = configuration.get('versionPreference') as VersionPreference;
10+
const id = versionPreference === VersionPreference.Stable ? 'Microsoft.DevProxy' : 'Microsoft.DevProxy.Beta';
11+
const message = vscode.window.setStatusBarMessage('Installing Dev Proxy...');
12+
13+
// we are on windows so we can use winget
14+
if (platform === 'win32') {
15+
// we first need to check if winget is installed, it is bundled with windows 11 but not windows 10
16+
try {
17+
await executeCommand('winget --version');
18+
} catch (error) {
19+
await vscode.window.showErrorMessage('Winget is not installed. Please install winget and try again.');
20+
return;
21+
}
22+
23+
// winget is installed so we can proceed with the installation
24+
try {
25+
await executeCommand(`winget install ${id} --silent`);
26+
const result = await vscode.window.showInformationMessage('Dev Proxy installed.', 'Reload');
27+
if (result === 'Reload') {
28+
await vscode.commands.executeCommand('workbench.action.reloadWindow');
29+
};
30+
} catch (error) {
31+
vscode.window.showErrorMessage(`Failed to install Dev Proxy.\n${error}`);
32+
}
33+
}
34+
35+
// we are on macos so we can use brew
36+
if (platform === 'darwin') {
37+
try {
38+
await executeCommand('brew tap microsoft/dev-proxy');
39+
await executeCommand(`brew install ${id}`);
40+
const result = await vscode.window.showInformationMessage('Dev Proxy installed.', 'Reload');
41+
if (result === 'Reload') {
42+
await vscode.commands.executeCommand('workbench.action.reloadWindow');
43+
};
44+
} catch (error) {
45+
vscode.window.showErrorMessage(`Failed to install Dev Proxy.\n${error}`);
46+
}
47+
}
48+
49+
if (platform === 'linux') {
50+
// we are on linux so we point the user to the documentation to install manually
51+
const url = 'https://aka.ms/devproxy/start';
52+
vscode.env.openExternal(vscode.Uri.parse(url));
53+
}
54+
55+
// remove the status bar message
56+
message.dispose();
957
}));
1058

1159
context.subscriptions.push(

src/detect.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import * as vscode from 'vscode';
2-
import { exec } from 'child_process';
31
import { DevProxyInstall } from './types';
42
import os from 'os';
53
import { VersionExeName, VersionPreference } from './enums';
4+
import { executeCommand } from './helpers';
65

76
export const getVersion = async (devProxyExe: string) => {
87
try {
@@ -13,19 +12,7 @@ export const getVersion = async (devProxyExe: string) => {
1312
}
1413
};
1514

16-
export const executeCommand = async (cmd: string): Promise<string> => {
17-
return new Promise((resolve, reject) => {
18-
exec(cmd, (error, stdout, stderr) => {
19-
if (error) {
20-
reject(`exec error: ${error}`);
21-
} else if (stderr) {
22-
reject(`stderr: ${stderr}`);
23-
} else {
24-
resolve(stdout);
25-
}
26-
});
27-
});
28-
};
15+
2916

3017
export const detectDevProxyInstall = async (versionPreference: VersionPreference): Promise<DevProxyInstall> => {
3118
const devProxyExe = getDevProxyExe(versionPreference);

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const activate = async (context: vscode.ExtensionContext): Promise<vscode
2020
registerDocumentListeners(context, collection);
2121
registerCodeActions(context);
2222
registerCodeLens(context);
23-
registerCommands(context);
23+
registerCommands(context, configuration);
2424

2525
const notification = handleStartNotification(context);
2626
processNotification(notification);

src/helpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import parse from 'json-to-ast';
3+
import { exec } from 'child_process';
34

45
export const getASTNode = (
56
children: parse.PropertyNode[],
@@ -82,3 +83,17 @@ export const sleep = (ms: number): Promise<void> => {
8283
setTimeout(resolve, ms);
8384
});
8485
};
86+
87+
export const executeCommand = async (cmd: string): Promise<string> => {
88+
return new Promise((resolve, reject) => {
89+
exec(cmd, (error, stdout, stderr) => {
90+
if (error) {
91+
reject(`exec error: ${error}`);
92+
} else if (stderr) {
93+
reject(`stderr: ${stderr}`);
94+
} else {
95+
resolve(stdout);
96+
}
97+
});
98+
});
99+
};

0 commit comments

Comments
 (0)