Skip to content

Commit 08e1b15

Browse files
authored
Add support for controlling Dev Proxy remotely. Closes #119 (#120)
1 parent d1cbdd6 commit 08e1b15

File tree

8 files changed

+269
-27
lines changed

8 files changed

+269
-27
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@ 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+
## Unreleased
9+
10+
### Added:
11+
12+
- Editor action: Start Dev Proxy
13+
- Editor action: Stop Dev Proxy
14+
- Editor action: Raise Mock Request
15+
- Editor action: Start Recording
16+
- Editor action: Stop Recording
17+
- Command: `dev-proxy-toolkit.start` - Starts Dev Proxy
18+
- Command: `dev-proxy-toolkit.stop` - Stops Dev Proxy
19+
- Command: `dev-proxy-toolkit.raise-mock` - Raises a mock request
20+
- Command: `dev-proxy-toolkit.record-start` - Starts recording Dev Proxy traffic
21+
- Command: `dev-proxy-toolkit.record-stop` - Stops recording Dev Proxy traffic
22+
- Setting: `dev-proxy-toolkit.newTerminal` - Determines if a new terminal should be created when starting Dev Proxy
23+
- Setting: `dev-proxy-toolkit.showTerminal` - Determines if the terminal should be shown when starting Dev Proxy
24+
- Setting: `dev-proxy-toolkit.closeTerminal` - Determines if the terminal should be closed when stopping Dev Proxy
25+
- Setting: `dev-proxy-toolkit.apiPort` - Port number used to communicate with Dev Proxy API
26+
- Diagnostics: Collection renamed to `dev-proxy-toolkit`
27+
28+
### Changed:
29+
30+
- Setting: `devproxytoolkit.versionPreference` renamed to `dev-proxy-toolkit.version`
31+
832
## [0.8.0] - 2024-09-06
933

1034
### Changed:

README.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,23 @@ The Dev Proxy Toolkit extension for Visual Studio Code makes it easy to create a
1111
1212
## Features
1313

14-
The following sections describe the features that the extension contributes to Visual Studio Code when installed
14+
The following sections describe the features that the extension contributes to Visual Studio Code when installed.
15+
16+
### Code Actions
17+
18+
- Update schema to match installed version of Dev Proxy
19+
20+
### Code Lenses
21+
22+
- Plugin documentation link
23+
24+
### Commands
25+
26+
- `Dev Proxy: Start` - Only available when Dev Proxy is not running
27+
- `Dev Proxy: Stop` - Only available when Dev Proxy is running
28+
- `Dev Proxy: Raise mock request` - Only available when Dev Proxy is running
29+
- `Dev Proxy: Start recording` - Only available when Dev Proxy is running
30+
- `Dev Proxy: Stop recording`- Only available when Dev Proxy is recording
1531

1632
### Diagnostics
1733

@@ -20,30 +36,28 @@ The following sections describe the features that the extension contributes to V
2036
- Check that schema matches installed version of Dev Proxy
2137
- Check that reporters are placed after plugins
2238

23-
### Code Actions
39+
### Editor Actions
2440

25-
- Update schema to match installed version of Dev Proxy
41+
Shown when the active document is a Dev Proxy configuration file
2642

27-
### Code Lenses
28-
29-
- Plugin documentation link
43+
- Start Dev Proxy
44+
- Stop Dev Proxy
45+
- Raise mock request
46+
- Start recording
47+
- Stop recording
3048

3149
### Notifications
3250

3351
- Not installed
3452
- New version detection
3553

36-
### Status Bar
37-
38-
- Display installed Dev Proxy version
39-
- Display waring when Dev Proxy is not latest version
40-
- Display tick if Dev Proxy is latest version (check based on `newVersionNotification` config setting)
41-
- Display radio tower when Dev Proxy is running
42-
- Display error is Dev Proxy is not installed
43-
4454
### Settings
4555

46-
- `devproxytoolkit.versionPreference` - Determines the version to use when Dev Proxy and Dev Proxy Beta are installed side by side. Can be `stable` (default) or `beta`.
56+
- `dev-proxy-toolkit.version` - Determines the version to use when Dev Proxy and Dev Proxy Beta are installed side by side. Can be `stable` (default) or `beta`.
57+
- `dev-proxy-toolkit.newTerminal` - Determines if Dev Proxy should be started in a new terminal. Can be `true` (default) or `false`.
58+
- `dev-proxy-toolkit.showTerminal` - Determines if the terminal should be shown when Dev Proxy is started. Can be `true` (default) or `false`.
59+
- `dev-proxy-toolkit.closeTerminal` - Determines if the terminal should be closed when Dev Proxy is stopped. Can be `true` (default) or `false`.
60+
- `dev-proxy-toolkit.apiPort` - Determines the port to use to communicate with Dev Proxy API. Default is `8897`.
4761

4862
### Snippets
4963

@@ -112,3 +126,11 @@ The following sections describe the features that the extension contributes to V
112126
| `devproxy-reporter-json` | JsonReporter instance |
113127
| `devproxy-reporter-markdown` | MarkdownReporter instance |
114128
| `devproxy-reporter-plain-text` | PlainTextReporter instance |
129+
130+
### Status Bar
131+
132+
- Display installed Dev Proxy version
133+
- Display waring when Dev Proxy is not latest version
134+
- Display tick if Dev Proxy is latest version (check based on `newVersionNotification` config setting in Dev Proxy configuration file)
135+
- Display radio tower when Dev Proxy is running
136+
- Display error is Dev Proxy is not installed

package.json

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,108 @@
2020
},
2121
"main": "./dist/extension.js",
2222
"contributes": {
23+
"commands": [
24+
{
25+
"command": "dev-proxy-toolkit.start",
26+
"title": "Start",
27+
"category": "Dev Proxy Toolkit",
28+
"icon": "$(debug-start)",
29+
"enablement": "!isDevProxyRunning"
30+
},
31+
{
32+
"command": "dev-proxy-toolkit.stop",
33+
"title": "Stop",
34+
"category": "Dev Proxy Toolkit",
35+
"icon": "$(debug-stop)",
36+
"enablement": "isDevProxyRunning"
37+
},
38+
{
39+
"command": "dev-proxy-toolkit.raise-mock",
40+
"title": "Raise mock request",
41+
"category": "Dev Proxy Toolkit",
42+
"icon": "$(cloud-download)",
43+
"enablement": "isDevProxyRunning"
44+
},
45+
{
46+
"command": "dev-proxy-toolkit.record-start",
47+
"title": "Start recording",
48+
"category": "Dev Proxy Toolkit",
49+
"icon": "$(circle-filled)",
50+
"enablement": "isDevProxyRunning && !isDevProxyRecording"
51+
},
52+
{
53+
"command": "dev-proxy-toolkit.record-stop",
54+
"title": "Stop recording",
55+
"category": "Dev Proxy Toolkit",
56+
"icon": "$(circle-slash)",
57+
"enablement": "isDevProxyRunning && isDevProxyRecording"
58+
}
59+
],
60+
"menus": {
61+
"editor/title": [
62+
{
63+
"command": "dev-proxy-toolkit.start",
64+
"group": "navigation@1",
65+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && !isDevProxyRunning"
66+
},
67+
{
68+
"command": "dev-proxy-toolkit.stop",
69+
"group": "navigation@2",
70+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning"
71+
},
72+
{
73+
"command": "dev-proxy-toolkit.raise-mock",
74+
"group": "navigation@3",
75+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning"
76+
},
77+
{
78+
"command": "dev-proxy-toolkit.record-start",
79+
"group": "navigation@4",
80+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning && !isDevProxyRecording"
81+
},
82+
{
83+
"command": "dev-proxy-toolkit.record-stop",
84+
"group": "navigation@5",
85+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning && isDevProxyRecording"
86+
}
87+
]
88+
},
2389
"snippets": [
2490
{
2591
"language": "json",
2692
"path": "./dist/snippets.json"
2793
}
2894
],
2995
"configuration": {
30-
"title": "Dev Proxy Toolkit",
3196
"properties": {
32-
"devproxytoolkit.versionPreference": {
97+
"dev-proxy-toolkit.version": {
3398
"type": "string",
34-
"enum": ["stable", "beta"],
99+
"enum": [
100+
"stable",
101+
"beta"
102+
],
35103
"default": "stable",
36-
"description": "Select the preferred version to be used when installed side by side."
104+
"description": "The preferred version to be used when installed side by side. (Requires a restart of extension to take effect)"
105+
},
106+
"dev-proxy-toolkit.newTerminal": {
107+
"type": "boolean",
108+
"default": true,
109+
"description": "Create a new terminal session when starting Dev Proxy."
110+
},
111+
"dev-proxy-toolkit.showTerminal": {
112+
"type": "boolean",
113+
"default": true,
114+
"description": "Show the terminal when starting Dev Proxy."
115+
},
116+
"dev-proxy-toolkit.closeTerminal": {
117+
"type": "boolean",
118+
"default": true,
119+
"description": "Close the terminal when stopping Dev Proxy."
120+
},
121+
"dev-proxy-toolkit.apiPort": {
122+
"type": "number",
123+
"default": 8897,
124+
"description": "The port number used to communicate with Dev Proxy API."
37125
}
38126
}
39127
}

src/commands.ts

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as vscode from 'vscode';
22
import { pluginDocs } from './constants';
33
import { VersionPreference } from './enums';
4-
import { executeCommand } from './helpers';
4+
import { executeCommand, isConfigFile } from './helpers';
5+
import { config } from 'process';
56

67
export const registerCommands = (context: vscode.ExtensionContext, configuration: vscode.WorkspaceConfiguration) => {
78
context.subscriptions.push(
89
vscode.commands.registerCommand('dev-proxy-toolkit.install', async (platform: NodeJS.Platform) => {
9-
const versionPreference = configuration.get('versionPreference') as VersionPreference;
10+
const versionPreference = configuration.get('version') as VersionPreference;
1011
const id = versionPreference === VersionPreference.Stable ? 'Microsoft.DevProxy' : 'Microsoft.DevProxy.Beta';
1112
const message = vscode.window.setStatusBarMessage('Installing Dev Proxy...');
1213

@@ -71,4 +72,90 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
7172
const url = 'https://aka.ms/devproxy/upgrade';
7273
vscode.env.openExternal(vscode.Uri.parse(url));
7374
}));
75+
76+
context.subscriptions.push(
77+
vscode.commands.registerCommand('dev-proxy-toolkit.start', async () => {
78+
const showTerminal = configuration.get('showTerminal') as boolean;
79+
const newTerminal = configuration.get('newTerminal') as boolean;
80+
81+
let terminal: vscode.Terminal;
82+
83+
if (!newTerminal && vscode.window.activeTerminal) {
84+
terminal = vscode.window.activeTerminal;
85+
} else {
86+
terminal = vscode.window.createTerminal('Dev Proxy');
87+
88+
showTerminal ? terminal.show() : terminal.hide();
89+
}
90+
91+
vscode.window.activeTextEditor && isConfigFile(vscode.window.activeTextEditor.document)
92+
? terminal.sendText(`devproxy --config-file "${vscode.window.activeTextEditor.document.uri.fsPath}"`)
93+
: terminal.sendText('devproxy');
94+
}));
95+
96+
context.subscriptions.push(
97+
vscode.commands.registerCommand('dev-proxy-toolkit.stop', async () => {
98+
const apiPort = configuration.get('apiPort') as number;
99+
await fetch(`http://localhost:${apiPort}/proxy/stopproxy`, {
100+
method: 'POST',
101+
headers: {
102+
'Content-Type': 'application/json'
103+
}
104+
});
105+
106+
const closeTerminal = configuration.get('closeTerminal') as boolean;
107+
if (closeTerminal) {
108+
vscode.window.terminals.forEach(terminal => {
109+
if (terminal.name === 'Dev Proxy') {
110+
terminal.dispose();
111+
}
112+
});
113+
}
114+
}));
115+
116+
context.subscriptions.push(
117+
vscode.commands.registerCommand('dev-proxy-toolkit.raise-mock', async () => {
118+
const apiPort = configuration.get('apiPort') as number;
119+
await fetch(`http://localhost:${apiPort}/proxy/raisemockrequest`, {
120+
method: 'POST',
121+
headers: {
122+
'Content-Type': 'application/json'
123+
}
124+
});
125+
vscode.window.showInformationMessage('Mock request raised');
126+
}));
127+
128+
context.subscriptions.push(
129+
vscode.commands.registerCommand('dev-proxy-toolkit.record-start', async () => {
130+
const apiPort = configuration.get('apiPort') as number;
131+
try {
132+
await fetch(`http://localhost:${apiPort}/proxy`, {
133+
method: 'POST',
134+
headers: {
135+
'Content-Type': 'application/json'
136+
},
137+
body: JSON.stringify({ recording: true })
138+
});
139+
vscode.commands.executeCommand('setContext', 'isDevProxyRecording', true);
140+
} catch {
141+
vscode.window.showErrorMessage('Failed to start recording');
142+
}
143+
}));
144+
145+
context.subscriptions.push(
146+
vscode.commands.registerCommand('dev-proxy-toolkit.record-stop', async () => {
147+
const apiPort = configuration.get('apiPort') as number;
148+
try {
149+
await fetch(`http://localhost:${apiPort}/proxy`, {
150+
method: 'POST',
151+
headers: {
152+
'Content-Type': 'application/json'
153+
},
154+
body: JSON.stringify({ recording: false })
155+
});
156+
vscode.commands.executeCommand('setContext', 'isDevProxyRecording', false);
157+
} catch {
158+
vscode.window.showErrorMessage('Failed to stop recording');
159+
}
160+
}));
74161
};

src/detect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DevProxyInstall } from './types';
22
import os from 'os';
33
import { VersionExeName, VersionPreference } from './enums';
44
import { executeCommand } from './helpers';
5+
import * as vscode from 'vscode';
56

67
export const getVersion = async (devProxyExe: string) => {
78
try {
@@ -12,8 +13,6 @@ export const getVersion = async (devProxyExe: string) => {
1213
}
1314
};
1415

15-
16-
1716
export const detectDevProxyInstall = async (versionPreference: VersionPreference): Promise<DevProxyInstall> => {
1817
const devProxyExe = getDevProxyExe(versionPreference);
1918
const version = await getVersion(devProxyExe);
@@ -23,6 +22,7 @@ export const detectDevProxyInstall = async (versionPreference: VersionPreference
2322
const outdatedVersion = await getOutdatedVersion(devProxyExe);
2423
const isOutdated = isInstalled && outdatedVersion !== '';
2524
const isRunning = await isDevProxyRunning(devProxyExe);
25+
vscode.commands.executeCommand('setContext', 'isDevProxyRunning', isRunning);
2626

2727
return {
2828
version,

0 commit comments

Comments
 (0)