From f414b59f13268a798591714d277fb1f74d7ac6be Mon Sep 17 00:00:00 2001 From: Garry Trinder Date: Fri, 11 Apr 2025 14:51:46 +0100 Subject: [PATCH 1/3] Add restart command. Closes #235 Closes #235 --- CHANGELOG.md | 1 + README.md | 1 + package.json | 22 ++++++++++++++++++ src/commands.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++++-- src/detect.ts | 4 ++-- 5 files changed, 85 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77474ad..01e3dba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added: - Command: `dev-proxy-toolkit.config-new` - Create new configuration file +- Command: `dev-proxy-toolkit.restart` - Restart Dev Proxy ## [0.20.0] - 2025-04-01 diff --git a/README.md b/README.md index 50a27bf..92754e0 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ The following sections describe the features that the extension contributes to V - `Dev Proxy Toolkit: Start` - Only available when Dev Proxy is not running - `Dev Proxy Toolkit: Stop` - Only available when Dev Proxy is running +- `Dev Proxy Toolkit: Restart` - Only available when Dev Proxy is running - `Dev Proxy Toolkit: Raise mock request` - Only available when Dev Proxy is running - `Dev Proxy Toolkit: Start recording` - Only available when Dev Proxy is running - `Dev Proxy Toolkit: Stop recording`- Only available when Dev Proxy is recording diff --git a/package.json b/package.json index 03dee3e..532eb96 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,13 @@ "icon": "$(debug-stop)", "enablement": "isDevProxyRunning" }, + { + "command": "dev-proxy-toolkit.restart", + "title": "Restart", + "category": "Dev Proxy Toolkit", + "icon": "$(debug-restart)", + "enablement": "isDevProxyRunning" + }, { "command": "dev-proxy-toolkit.raise-mock", "title": "Raise mock request", @@ -83,6 +90,21 @@ "group": "navigation@2", "when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning" }, + { + "command": "dev-proxy-toolkit.restart", + "group": "navigation@2", + "when": "!activeEditorIsDirty && isDevProxyConfigFile && isDevProxyRunning" + }, + { + "command": "dev-proxy-toolkit.config-open", + "group": "navigation@3", + "when": "!activeEditorIsDirty && isDevProxyConfigFile && !isDevProxyRunning" + }, + { + "command": "dev-proxy-toolkit.config-new", + "group": "navigation@3", + "when": "!activeEditorIsDirty && isDevProxyConfigFile && !isDevProxyRunning" + }, { "command": "dev-proxy-toolkit.raise-mock", "group": "navigation@3", diff --git a/src/commands.ts b/src/commands.ts index 26c4444..b5fab7e 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import { pluginDocs } from './constants'; import { VersionExeName, VersionPreference } from './enums'; import { executeCommand, isConfigFile } from './helpers'; +import { isDevProxyRunning } from './detect'; export const registerCommands = (context: vscode.ExtensionContext, configuration: vscode.WorkspaceConfiguration) => { context.subscriptions.push( @@ -104,6 +105,9 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration context.subscriptions.push( vscode.commands.registerCommand('dev-proxy-toolkit.stop', async () => { const apiPort = configuration.get('apiPort') as number; + const versionPreference = configuration.get('version') as VersionPreference; + const exeName = versionPreference === VersionPreference.Stable ? VersionExeName.Stable : VersionExeName.Beta; + await fetch(`http://localhost:${apiPort}/proxy/stopproxy`, { method: 'POST', headers: { @@ -115,8 +119,7 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration if (closeTerminal) { const checkProxyStatus = async () => { try { - const response = await fetch(`http://localhost:${apiPort}/proxy`); - return response.ok; + return await isDevProxyRunning(exeName); } catch { return false; } @@ -142,6 +145,60 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration } })); + context.subscriptions.push( + vscode.commands.registerCommand('dev-proxy-toolkit.restart', async () => { + const apiPort = configuration.get('apiPort') as number; + const versionPreference = configuration.get('version') as VersionPreference; + const exeName = versionPreference === VersionPreference.Stable ? VersionExeName.Stable : VersionExeName.Beta; + + try { + await fetch(`http://localhost:${apiPort}/proxy/stopproxy`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + }); + + const checkProxyStatus = async () => { + try { + return await isDevProxyRunning(exeName); + } catch { + return false; + } + }; + + const waitForProxyToStop = async () => { + let isRunning = true; + while (isRunning) { + isRunning = await checkProxyStatus(); + if (isRunning) { + await new Promise(resolve => setTimeout(resolve, 1000)); + } + } + }; + + await waitForProxyToStop(); + + const showTerminal = configuration.get('showTerminal') as boolean; + + let terminal: vscode.Terminal; + + if (vscode.window.activeTerminal) { + terminal = vscode.window.activeTerminal; + } else { + terminal = vscode.window.createTerminal('Dev Proxy'); + + showTerminal ? terminal.show() : terminal.hide(); + } + + vscode.window.activeTextEditor && isConfigFile(vscode.window.activeTextEditor.document) + ? terminal.sendText(`devproxy --config-file "${vscode.window.activeTextEditor.document.uri.fsPath}"`) + : terminal.sendText('devproxy'); + } catch { + vscode.window.showErrorMessage('Failed to restart Dev Proxy'); + } + })); + context.subscriptions.push( vscode.commands.registerCommand('dev-proxy-toolkit.raise-mock', async () => { const apiPort = configuration.get('apiPort') as number; diff --git a/src/detect.ts b/src/detect.ts index c01f1ea..e755bb8 100644 --- a/src/detect.ts +++ b/src/detect.ts @@ -51,8 +51,8 @@ export const isDevProxyRunning = async (devProxyExe: string): Promise = const processId = await executeCommand(`pwsh.exe -c "(Get-Process ${devProxyExe} -ErrorAction SilentlyContinue).Id"`); return processId.trim() !== ''; }; - if (platform === 'darwin') { - const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk \'{print $2}\'"`); + if (platform === 'darwin' || platform === 'linux') { + const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk '{print $2}'"`); return processId.trim() !== ''; }; return false; From c87ea2d580dadc2b8c12965d1dccd474e365514c Mon Sep 17 00:00:00 2001 From: Garry Trinder Date: Fri, 11 Apr 2025 16:41:00 +0100 Subject: [PATCH 2/3] Fix escaping in command for detecting running DevProxy on Unix-like systems --- src/detect.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/detect.ts b/src/detect.ts index e755bb8..d0bcf47 100644 --- a/src/detect.ts +++ b/src/detect.ts @@ -52,7 +52,7 @@ export const isDevProxyRunning = async (devProxyExe: string): Promise = return processId.trim() !== ''; }; if (platform === 'darwin' || platform === 'linux') { - const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk '{print $2}'"`); + const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk \'{print $2}\'"`); return processId.trim() !== ''; }; return false; From ce095137b3fd5de28a4ad4d67403144c24e677b5 Mon Sep 17 00:00:00 2001 From: Garry Trinder Date: Fri, 11 Apr 2025 16:44:08 +0100 Subject: [PATCH 3/3] Refactor isDevProxyRunning to separate platform checks for darwin and linux --- src/detect.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/detect.ts b/src/detect.ts index d0bcf47..87d14bc 100644 --- a/src/detect.ts +++ b/src/detect.ts @@ -51,10 +51,14 @@ export const isDevProxyRunning = async (devProxyExe: string): Promise = const processId = await executeCommand(`pwsh.exe -c "(Get-Process ${devProxyExe} -ErrorAction SilentlyContinue).Id"`); return processId.trim() !== ''; }; - if (platform === 'darwin' || platform === 'linux') { - const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk \'{print $2}\'"`); + if (platform === 'darwin') { + const processId = await executeCommand(`$SHELL -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk '{print $2}'"`); return processId.trim() !== ''; }; + if (platform === 'linux') { + const processId = await executeCommand(`/bin/bash -c "ps -ef | grep ${devProxyExe} | grep -v grep | awk '{print $2}'"`); + return processId.trim() !== ''; + } return false; };