Skip to content

Commit 8aac9f2

Browse files
authored
Add proxy running detection (#49)
1 parent 298dadb commit 8aac9f2

File tree

9 files changed

+205
-8
lines changed

9 files changed

+205
-8
lines changed

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- name: Install Node.js
1818
uses: actions/setup-node@v4
1919
with:
20-
node-version: 21.x
20+
node-version: 18.x
2121
- run: npm install
2222
- run: xvfb-run -a npm test
2323
if: runner.os == 'Linux'

package-lock.json

Lines changed: 140 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@types/json-to-ast": "^2.1.4",
5050
"@types/mocha": "^10.0.6",
5151
"@types/node": "^20.11.17",
52+
"@types/sinon": "^17.0.3",
5253
"@types/vscode": "^1.85.0",
5354
"@typescript-eslint/eslint-plugin": "^6.19.0",
5455
"@typescript-eslint/parser": "^6.19.0",
@@ -57,6 +58,7 @@
5758
"copy-webpack-plugin": "^12.0.2",
5859
"eslint": "^8.56.0",
5960
"gts": "^5.2.0",
61+
"sinon": "^17.0.1",
6062
"ts-loader": "^9.5.1",
6163
"typescript": "^5.3.3",
6264
"webpack": "^5.89.0",

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,5 +204,6 @@ export const testDevProxyInstall: DevProxyInstall = {
204204
isLatest: true,
205205
latestVersion: '0.14.1',
206206
platform: 'win32',
207-
version: '0.14.1'
207+
version: '0.14.1',
208+
isRunning: false
208209
};

src/detect.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export const detectDevProxyInstall = async (): Promise<DevProxyInstall> => {
6363
const platform = os.platform();
6464
const latestVersion = await getLatestVersion();
6565
const isLatest = latestVersion === version;
66+
const isRunning = await isDevProxyRunning();
6667

6768
return {
6869
filePath,
@@ -71,12 +72,27 @@ export const detectDevProxyInstall = async (): Promise<DevProxyInstall> => {
7172
isBeta,
7273
platform,
7374
latestVersion,
74-
isLatest
75+
isLatest,
76+
isRunning
7577
};
7678
};
7779

7880
export const getLatestVersion = async (): Promise<string> => {
7981
const request = await fetch('https://api.github.com/repos/microsoft/dev-proxy/releases/latest');
8082
const release = await request.json() as Release;
8183
return release.tag_name.replace('v', '');
84+
};
85+
86+
export const isDevProxyRunning = async (): Promise<boolean> => {
87+
const platform = os.platform();
88+
89+
if (platform === 'win32') {
90+
const processId = await executeCommand('pwsh.exe -c "(Get-Process devproxy -ErrorAction SilentlyContinue).Id"');
91+
return processId.trim() !== '';
92+
};
93+
if (platform === 'darwin') {
94+
const processId = await executeCommand('$SHELL -c "ps -ef | grep devproxy | grep -v grep | awk \'{print $2}\'"');
95+
return processId.trim() !== '';
96+
};
97+
return false;
8298
};

src/extension.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { registerCommands } from './commands';
33
import { handleStartNotification, processNotification } from './notifications';
44
import { registerDocumentListeners } from './documents';
55
import { registerCodeLens } from './codelens';
6-
import { createStatusBar, updateStatusBar } from './statusbar';
6+
import { createStatusBar, statusBarLoop, updateStatusBar } from './statusbar';
77
import { registerCodeActions } from './codeactions';
88
import { updateGlobalState } from './state';
99

@@ -12,14 +12,19 @@ export const activate = async (context: vscode.ExtensionContext): Promise<vscode
1212
await updateGlobalState(context);
1313

1414
const collection = vscode.languages.createDiagnosticCollection('Dev Proxy');
15+
1516
registerDocumentListeners(context, collection);
1617
registerCodeActions(context);
1718
registerCodeLens(context);
1819
registerCommands(context);
20+
1921
const notification = handleStartNotification(context);
2022
processNotification(notification);
21-
23+
2224
updateStatusBar(context, statusBar);
25+
26+
setInterval(() => statusBarLoop(context, statusBar), 5000);
27+
2328
return context;
2429
};
2530

src/statusbar.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import { DevProxyInstall } from './types';
3+
import { isDevProxyRunning } from './detect';
34

45
export const createStatusBar = (context: vscode.ExtensionContext): vscode.StatusBarItem => {
56
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
@@ -34,5 +35,18 @@ export const handleStatusBarUpdate = (context: vscode.ExtensionContext, statusBa
3435
statusBar.text = `$(warning) Dev Proxy ${devProxyInstall.version}`;
3536
statusBar.tooltip = `An update is available`;
3637
}
38+
if (devProxyInstall.isRunning) {
39+
statusBar.text = `$(radio-tower) Dev Proxy ${devProxyInstall.version}`;
40+
statusBar.tooltip = 'Dev Proxy is active';
41+
statusBar.backgroundColor = new vscode.ThemeColor('statusBarItem.prominentBackground');
42+
statusBar.color = new vscode.ThemeColor('statusBarItem.prominentForeground');
43+
}
3744
return statusBar;
38-
};
45+
};
46+
47+
export const statusBarLoop = async (context: vscode.ExtensionContext, statusBar: vscode.StatusBarItem) => {
48+
const isRunning = await isDevProxyRunning();
49+
const globalState = context.globalState.get<DevProxyInstall>('devProxyInstall');
50+
await context.globalState.update('devProxyInstall', { ...globalState, isRunning });
51+
updateStatusBar(context, statusBar);
52+
};

src/test/extension.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import * as path from 'path';
1111
import parse from 'json-to-ast';
1212
import { createCodeLensForPluginNodes } from '../codelens';
1313
import { handleStartNotification } from '../notifications';
14-
import { handleStatusBarUpdate } from '../statusbar';
14+
import { handleStatusBarUpdate, statusBarLoop } from '../statusbar';
1515
import { testDevProxyInstall } from '../constants';
16+
import * as sinon from 'sinon';
17+
import * as detect from '../detect';
1618

1719
suite('extension', () => {
1820

@@ -487,6 +489,22 @@ suite('statusbar', () => {
487489
const actual = updatedStatusBar.text;
488490
assert.strictEqual(actual, expected);
489491
});
492+
493+
test('should show radio tower icon when devproxy is running', async () => {
494+
const stub = sinon.stub(detect, 'isDevProxyRunning').resolves(true);
495+
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
496+
const statusBar = vscode.window.createStatusBarItem(
497+
vscode.StatusBarAlignment.Right,
498+
100
499+
);
500+
await statusBarLoop(context, statusBar);
501+
502+
const expected = '$(radio-tower) Dev Proxy 0.14.1';
503+
const actual = statusBar.text;
504+
stub.restore();
505+
assert.strictEqual(actual, expected);
506+
});
507+
490508
});
491509

492510
suite('schema', () => {
@@ -550,6 +568,6 @@ suite('schema', () => {
550568
const expected = 0;
551569
const actual = diagnostics.length;
552570
assert.deepStrictEqual(actual, expected);
553-
});
571+
});
554572

555573
});

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type DevProxyInstall = {
2525
platform: NodeJS.Platform;
2626
latestVersion: string;
2727
isLatest: boolean;
28+
isRunning: boolean;
2829
};
2930

3031
export type Release = {

0 commit comments

Comments
 (0)