Skip to content

Commit 0a41c6f

Browse files
authored
Add restart command. Closes #235 (#238)
1 parent 9f27491 commit 0a41c6f

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Added:
1313

1414
- Command: `dev-proxy-toolkit.config-new` - Create new configuration file
15+
- Command: `dev-proxy-toolkit.restart` - Restart Dev Proxy
1516

1617
## [0.20.0] - 2025-04-01
1718

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The following sections describe the features that the extension contributes to V
2525

2626
- `Dev Proxy Toolkit: Start` - Only available when Dev Proxy is not running
2727
- `Dev Proxy Toolkit: Stop` - Only available when Dev Proxy is running
28+
- `Dev Proxy Toolkit: Restart` - Only available when Dev Proxy is running
2829
- `Dev Proxy Toolkit: Raise mock request` - Only available when Dev Proxy is running
2930
- `Dev Proxy Toolkit: Start recording` - Only available when Dev Proxy is running
3031
- `Dev Proxy Toolkit: Stop recording`- Only available when Dev Proxy is recording

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
"icon": "$(debug-stop)",
3636
"enablement": "isDevProxyRunning"
3737
},
38+
{
39+
"command": "dev-proxy-toolkit.restart",
40+
"title": "Restart",
41+
"category": "Dev Proxy Toolkit",
42+
"icon": "$(debug-restart)",
43+
"enablement": "isDevProxyRunning"
44+
},
3845
{
3946
"command": "dev-proxy-toolkit.raise-mock",
4047
"title": "Raise mock request",
@@ -83,6 +90,21 @@
8390
"group": "navigation@2",
8491
"when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning"
8592
},
93+
{
94+
"command": "dev-proxy-toolkit.restart",
95+
"group": "navigation@2",
96+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning"
97+
},
98+
{
99+
"command": "dev-proxy-toolkit.config-open",
100+
"group": "navigation@3",
101+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && !isDevProxyRunning"
102+
},
103+
{
104+
"command": "dev-proxy-toolkit.config-new",
105+
"group": "navigation@3",
106+
"when": "!activeEditorIsDirty && isDevProxyConfigFile && !isDevProxyRunning"
107+
},
86108
{
87109
"command": "dev-proxy-toolkit.raise-mock",
88110
"group": "navigation@3",

src/commands.ts

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

67
export const registerCommands = (context: vscode.ExtensionContext, configuration: vscode.WorkspaceConfiguration) => {
78
context.subscriptions.push(
@@ -104,6 +105,9 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
104105
context.subscriptions.push(
105106
vscode.commands.registerCommand('dev-proxy-toolkit.stop', async () => {
106107
const apiPort = configuration.get('apiPort') as number;
108+
const versionPreference = configuration.get('version') as VersionPreference;
109+
const exeName = versionPreference === VersionPreference.Stable ? VersionExeName.Stable : VersionExeName.Beta;
110+
107111
await fetch(`http://localhost:${apiPort}/proxy/stopproxy`, {
108112
method: 'POST',
109113
headers: {
@@ -115,8 +119,7 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
115119
if (closeTerminal) {
116120
const checkProxyStatus = async () => {
117121
try {
118-
const response = await fetch(`http://localhost:${apiPort}/proxy`);
119-
return response.ok;
122+
return await isDevProxyRunning(exeName);
120123
} catch {
121124
return false;
122125
}
@@ -142,6 +145,60 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
142145
}
143146
}));
144147

148+
context.subscriptions.push(
149+
vscode.commands.registerCommand('dev-proxy-toolkit.restart', async () => {
150+
const apiPort = configuration.get('apiPort') as number;
151+
const versionPreference = configuration.get('version') as VersionPreference;
152+
const exeName = versionPreference === VersionPreference.Stable ? VersionExeName.Stable : VersionExeName.Beta;
153+
154+
try {
155+
await fetch(`http://localhost:${apiPort}/proxy/stopproxy`, {
156+
method: 'POST',
157+
headers: {
158+
'Content-Type': 'application/json'
159+
}
160+
});
161+
162+
const checkProxyStatus = async () => {
163+
try {
164+
return await isDevProxyRunning(exeName);
165+
} catch {
166+
return false;
167+
}
168+
};
169+
170+
const waitForProxyToStop = async () => {
171+
let isRunning = true;
172+
while (isRunning) {
173+
isRunning = await checkProxyStatus();
174+
if (isRunning) {
175+
await new Promise(resolve => setTimeout(resolve, 1000));
176+
}
177+
}
178+
};
179+
180+
await waitForProxyToStop();
181+
182+
const showTerminal = configuration.get('showTerminal') as boolean;
183+
184+
let terminal: vscode.Terminal;
185+
186+
if (vscode.window.activeTerminal) {
187+
terminal = vscode.window.activeTerminal;
188+
} else {
189+
terminal = vscode.window.createTerminal('Dev Proxy');
190+
191+
showTerminal ? terminal.show() : terminal.hide();
192+
}
193+
194+
vscode.window.activeTextEditor && isConfigFile(vscode.window.activeTextEditor.document)
195+
? terminal.sendText(`devproxy --config-file "${vscode.window.activeTextEditor.document.uri.fsPath}"`)
196+
: terminal.sendText('devproxy');
197+
} catch {
198+
vscode.window.showErrorMessage('Failed to restart Dev Proxy');
199+
}
200+
}));
201+
145202
context.subscriptions.push(
146203
vscode.commands.registerCommand('dev-proxy-toolkit.raise-mock', async () => {
147204
const apiPort = configuration.get('apiPort') as number;

src/detect.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ export const isDevProxyRunning = async (devProxyExe: string): Promise<boolean> =
5252
return processId.trim() !== '';
5353
};
5454
if (platform === 'darwin') {
55-
const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk \'{print $2}\'"`);
55+
const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk '{print $2}'"`);
5656
return processId.trim() !== '';
5757
};
58+
if (platform === 'linux') {
59+
const processId = await executeCommand(`/bin/bash -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk '{print $2}'"`);
60+
return processId.trim() !== '';
61+
}
5862
return false;
5963
};
6064

0 commit comments

Comments
 (0)