Skip to content

Commit

Permalink
Support Dev Proxy install from package managers. Closes #82
Browse files Browse the repository at this point in the history
Closes #82
  • Loading branch information
garrytrinder committed May 28, 2024
1 parent 9541ea3 commit b991bd4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 21 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.4.0] - 2024-05-30

### Changed:

- Notification: Support for installing Dev Proxy via package manager when not installed

## [0.3.1] - 2024-05-22

### Fixed
### Fixed:

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "dev-proxy-toolkit",
"displayName": "Dev Proxy Toolkit",
"description": "Makes it easy to create and update Dev Proxy configuration files.",
"version": "0.3.1",
"version": "0.4.0",
"publisher": "garrytrinder",
"engines": {
"vscode": "^1.85.0"
Expand Down
54 changes: 51 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
import * as vscode from 'vscode';
import { pluginDocs } from './constants';
import { VersionPreference } from './enums';
import { executeCommand } from './helpers';

export const registerCommands = (context: vscode.ExtensionContext) => {
export const registerCommands = (context: vscode.ExtensionContext, configuration: vscode.WorkspaceConfiguration) => {
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.install', async (platform: NodeJS.Platform) => {
const url = `https://aka.ms/devproxy/install/${platform === 'darwin' ? 'macos' : 'windows'}`;
vscode.env.openExternal(vscode.Uri.parse(url));
const versionPreference = configuration.get('versionPreference') as VersionPreference;
const id = versionPreference === VersionPreference.Stable ? 'Microsoft.DevProxy' : 'Microsoft.DevProxy.Beta';
const message = vscode.window.setStatusBarMessage('Installing Dev Proxy...');

// we are on windows so we can use winget
if (platform === 'win32') {
// we first need to check if winget is installed, it is bundled with windows 11 but not windows 10
try {
await executeCommand('winget --version');
} catch (error) {
await vscode.window.showErrorMessage('Winget is not installed. Please install winget and try again.');
return;
}

// winget is installed so we can proceed with the installation
try {
await executeCommand(`winget install ${id} --silent`);
const result = await vscode.window.showInformationMessage('Dev Proxy installed.', 'Reload');
if (result === 'Reload') {
await vscode.commands.executeCommand('workbench.action.reloadWindow');
};
} catch (error) {
vscode.window.showErrorMessage(`Failed to install Dev Proxy.\n${error}`);
}
}

// we are on macos so we can use brew
if (platform === 'darwin') {
try {
await executeCommand('brew tap microsoft/dev-proxy');
await executeCommand(`brew install ${id}`);
const result = await vscode.window.showInformationMessage('Dev Proxy installed.', 'Reload');
if (result === 'Reload') {
await vscode.commands.executeCommand('workbench.action.reloadWindow');
};
} catch (error) {
vscode.window.showErrorMessage(`Failed to install Dev Proxy.\n${error}`);
}
}

if (platform === 'linux') {
// we are on linux so we point the user to the documentation to install manually
const url = 'https://aka.ms/devproxy/start';
vscode.env.openExternal(vscode.Uri.parse(url));
}

// remove the status bar message
message.dispose();
}));

context.subscriptions.push(
Expand Down
17 changes: 2 additions & 15 deletions src/detect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as vscode from 'vscode';
import { exec } from 'child_process';
import { DevProxyInstall } from './types';
import os from 'os';
import { VersionExeName, VersionPreference } from './enums';
import { executeCommand } from './helpers';

export const getVersion = async (devProxyExe: string) => {
try {
Expand All @@ -13,19 +12,7 @@ export const getVersion = async (devProxyExe: string) => {
}
};

export const executeCommand = async (cmd: string): Promise<string> => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(`exec error: ${error}`);
} else if (stderr) {
reject(`stderr: ${stderr}`);
} else {
resolve(stdout);
}
});
});
};


export const detectDevProxyInstall = async (versionPreference: VersionPreference): Promise<DevProxyInstall> => {
const devProxyExe = getDevProxyExe(versionPreference);
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const activate = async (context: vscode.ExtensionContext): Promise<vscode
registerDocumentListeners(context, collection);
registerCodeActions(context);
registerCodeLens(context);
registerCommands(context);
registerCommands(context, configuration);

const notification = handleStartNotification(context);
processNotification(notification);
Expand Down
15 changes: 15 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import parse from 'json-to-ast';
import { exec } from 'child_process';

export const getASTNode = (
children: parse.PropertyNode[],
Expand Down Expand Up @@ -82,3 +83,17 @@ export const sleep = (ms: number): Promise<void> => {
setTimeout(resolve, ms);
});
};

export const executeCommand = async (cmd: string): Promise<string> => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(`exec error: ${error}`);
} else if (stderr) {
reject(`stderr: ${stderr}`);
} else {
resolve(stdout);
}
});
});
};

0 comments on commit b991bd4

Please sign in to comment.