@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
2
2
import { pluginDocs } from './constants' ;
3
3
import { VersionExeName , VersionPreference } from './enums' ;
4
4
import { executeCommand , isConfigFile } from './helpers' ;
5
+ import { isDevProxyRunning } from './detect' ;
5
6
6
7
export const registerCommands = ( context : vscode . ExtensionContext , configuration : vscode . WorkspaceConfiguration ) => {
7
8
context . subscriptions . push (
@@ -104,6 +105,9 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
104
105
context . subscriptions . push (
105
106
vscode . commands . registerCommand ( 'dev-proxy-toolkit.stop' , async ( ) => {
106
107
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
+
107
111
await fetch ( `http://localhost:${ apiPort } /proxy/stopproxy` , {
108
112
method : 'POST' ,
109
113
headers : {
@@ -115,8 +119,7 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
115
119
if ( closeTerminal ) {
116
120
const checkProxyStatus = async ( ) => {
117
121
try {
118
- const response = await fetch ( `http://localhost:${ apiPort } /proxy` ) ;
119
- return response . ok ;
122
+ return await isDevProxyRunning ( exeName ) ;
120
123
} catch {
121
124
return false ;
122
125
}
@@ -142,6 +145,60 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
142
145
}
143
146
} ) ) ;
144
147
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
+
145
202
context . subscriptions . push (
146
203
vscode . commands . registerCommand ( 'dev-proxy-toolkit.raise-mock' , async ( ) => {
147
204
const apiPort = configuration . get ( 'apiPort' ) as number ;
0 commit comments