@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
22import { pluginDocs } from './constants' ;
33import { VersionExeName , VersionPreference } from './enums' ;
44import { executeCommand , isConfigFile } from './helpers' ;
5+ import { isDevProxyRunning } from './detect' ;
56
67export 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 ;
0 commit comments