-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommands.ts
More file actions
198 lines (175 loc) · 8.51 KB
/
commands.ts
File metadata and controls
198 lines (175 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import * as vscode from 'vscode';
import { pluginDocs } from './constants';
import { VersionExeName, VersionPreference } from './enums';
import { executeCommand, isConfigFile } from './helpers';
export const registerCommands = (context: vscode.ExtensionContext, configuration: vscode.WorkspaceConfiguration) => {
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.install', async (platform: NodeJS.Platform) => {
const versionPreference = configuration.get('version') as VersionPreference;
const message = vscode.window.setStatusBarMessage('Installing Dev Proxy...');
// we are on windows so we can use winget
if (platform === 'win32') {
const id = versionPreference === VersionPreference.Stable ? 'Microsoft.DevProxy' : 'Microsoft.DevProxy.Beta';
// we first need to check if winget is installed, it is bundled with windows 11 but not windows 10
try {
await executeCommand('winget --version');
} catch (error) {
await vscode.window.showErrorMessage('Winget is not installed. Please install winget and try again.');
return;
}
// winget is installed so we can proceed with the installation
try {
await executeCommand(`winget install ${id} --silent`);
const result = await vscode.window.showInformationMessage('Dev Proxy installed.', 'Reload');
if (result === 'Reload') {
await vscode.commands.executeCommand('workbench.action.reloadWindow');
};
} catch (error) {
vscode.window.showErrorMessage(`Failed to install Dev Proxy.\n${error}`);
}
}
// we are on macos so we can use brew
if (platform === 'darwin') {
const id = versionPreference === VersionPreference.Stable ? 'dev-proxy' : 'dev-proxy-beta';
// check if brew is installed
try {
await executeCommand('brew --version');
} catch (error) {
await vscode.window.showErrorMessage('Homebrew is not installed. Please install brew and try again.');
return;
}
try {
await executeCommand('brew tap dotnet/dev-proxy');
await executeCommand(`brew install ${id}`);
const result = await vscode.window.showInformationMessage('Dev Proxy installed.', 'Reload');
if (result === 'Reload') {
await vscode.commands.executeCommand('workbench.action.reloadWindow');
};
} catch (error) {
vscode.window.showErrorMessage(`Failed to install Dev Proxy.\n${error}`);
}
}
if (platform === 'linux') {
// we are on linux so we point the user to the documentation to install manually
const url = 'https://aka.ms/devproxy/start/linux';
vscode.env.openExternal(vscode.Uri.parse(url));
}
// remove the status bar message
message.dispose();
}));
context.subscriptions.push(
vscode.commands.registerCommand(
'dev-proxy-toolkit.openPluginDoc',
pluginName => {
const target = vscode.Uri.parse(pluginDocs[pluginName].url);
vscode.env.openExternal(target);
}
)
);
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.upgrade', async () => {
const url = 'https://aka.ms/devproxy/upgrade';
vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.start', async () => {
const showTerminal = configuration.get('showTerminal') as boolean;
const newTerminal = configuration.get('newTerminal') as boolean;
let terminal: vscode.Terminal;
if (!newTerminal && 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');
}));
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.stop', async () => {
const apiPort = configuration.get('apiPort') as number;
await fetch(`http://localhost:${apiPort}/proxy/stopproxy`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
const closeTerminal = configuration.get('closeTerminal') as boolean;
if (closeTerminal) {
const checkProxyStatus = async () => {
try {
const response = await fetch(`http://localhost:${apiPort}/proxy`);
return response.ok;
} 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();
vscode.window.terminals.forEach(terminal => {
if (terminal.name === 'Dev Proxy') {
terminal.dispose();
}
});
}
}));
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.raise-mock', async () => {
const apiPort = configuration.get('apiPort') as number;
await fetch(`http://localhost:${apiPort}/proxy/mockrequest`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
});
vscode.window.showInformationMessage('Mock request raised');
}));
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.record-start', async () => {
const apiPort = configuration.get('apiPort') as number;
try {
await fetch(`http://localhost:${apiPort}/proxy`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ recording: true })
});
vscode.commands.executeCommand('setContext', 'isDevProxyRecording', true);
} catch {
vscode.window.showErrorMessage('Failed to start recording');
}
}));
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.record-stop', async () => {
const apiPort = configuration.get('apiPort') as number;
try {
await fetch(`http://localhost:${apiPort}/proxy`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ recording: false })
});
vscode.commands.executeCommand('setContext', 'isDevProxyRecording', false);
} catch {
vscode.window.showErrorMessage('Failed to stop recording');
}
}));
context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.config-open', async () => {
const versionPreference = configuration.get('version') as VersionPreference;
versionPreference === VersionPreference.Stable
? await executeCommand(`${VersionExeName.Stable} config open`)
: await executeCommand(`${VersionExeName.Beta} config open`);
}));
};