Skip to content

Commit 108096e

Browse files
authored
Add proxy version to status bar (#47)
1 parent 3ff71c5 commit 108096e

File tree

4 files changed

+107
-2
lines changed

4 files changed

+107
-2
lines changed

src/detect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const detectDevProxyInstall = async (): Promise<DevProxyInstall> => {
7676
};
7777

7878
export const getLatestVersion = async (): Promise<string> => {
79-
const request = await fetch('https://api.github.com/repos/microsoft/dev-proxy/releases');
79+
const request = await fetch('https://api.github.com/repos/microsoft/dev-proxy/releases/latest');
8080
const release = await request.json() as Release;
8181
return release.tag_name.replace('v', '');
8282
};

src/extension.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ import { detectDevProxyInstall } from './detect';
44
import { handleStartNotification, processNotification } from './notifications';
55
import { registerDocumentListeners } from './documents';
66
import { registerCodeLens } from './codelens';
7+
import { createStatusBar, updateStatusBar } from './statusbar';
78

89
export const activate = async (context: vscode.ExtensionContext) => {
910
const collection = vscode.languages.createDiagnosticCollection('Dev Proxy');
11+
let statusBar = createStatusBar(context);
1012
registerDocumentListeners(context, collection);
1113
registerCommands(context);
1214
registerCodeLens(context);
13-
15+
1416
const devProxyInstall = await detectDevProxyInstall();
1517
const notification = handleStartNotification(devProxyInstall);
1618
processNotification(notification);
19+
updateStatusBar(statusBar, devProxyInstall);
1720
};
1821

1922
export const deactivate = () => { };

src/statusbar.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as vscode from 'vscode';
2+
import { DevProxyInstall } from './types';
3+
4+
export const createStatusBar = (context: vscode.ExtensionContext): vscode.StatusBarItem => {
5+
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
6+
statusBar.text = '$(sync~spin) Dev Proxy';
7+
statusBar.show();
8+
context.subscriptions.push(statusBar);
9+
return statusBar;
10+
};
11+
12+
export const updateStatusBar = (statusBar: vscode.StatusBarItem, devProxyInstall: DevProxyInstall) => {
13+
statusBar = handleStatusBarUpdate(statusBar, devProxyInstall);
14+
};
15+
16+
export const handleStatusBarUpdate = (statusBar: vscode.StatusBarItem, devProxyInstall: DevProxyInstall): vscode.StatusBarItem => {
17+
if (devProxyInstall.isInstalled) {
18+
statusBar.backgroundColor = new vscode.ThemeColor('statusBar.background');
19+
statusBar.color = new vscode.ThemeColor('statusBar.foreground');
20+
statusBar.text = `$(check) Dev Proxy ${devProxyInstall.version}`;
21+
statusBar.tooltip = `You have the latest version installed`;
22+
}
23+
if (!devProxyInstall.isInstalled) {
24+
statusBar.backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground');
25+
statusBar.color = new vscode.ThemeColor('statusBarItem.errorForeground');
26+
statusBar.text = `$(error) Dev Proxy`;
27+
statusBar.tooltip = `Dev Proxy is not installed`;
28+
}
29+
if (devProxyInstall.isInstalled && !devProxyInstall.isLatest) {
30+
statusBar.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
31+
statusBar.color = new vscode.ThemeColor('statusBarItem.warningForeground');
32+
statusBar.text = `$(warning) Dev Proxy ${devProxyInstall.version}`;
33+
statusBar.tooltip = `An update is available`;
34+
}
35+
return statusBar;
36+
};

src/test/extension.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import parse from 'json-to-ast';
1212
import { createCodeLensForPluginNodes } from '../codelens';
1313
import { DevProxyInstall } from '../types';
1414
import { handleStartNotification } from '../notifications';
15+
import { handleStatusBarUpdate } from '../statusbar';
1516

1617
suite('urlsToWatch', () => {
1718
test('should show error when opening document with no urlsToWatch found', async () => {
@@ -335,4 +336,69 @@ suite('notifications', () => {
335336
const actual = notification !== undefined && notification().message;
336337
assert.strictEqual(actual, expected);
337338
});
339+
});
340+
341+
suite('statusbar', () => {
342+
test('should show error statusbar when devproxy is not installed', async () => {
343+
const devProxyInstall: DevProxyInstall = {
344+
filePath: '',
345+
version: '',
346+
platform: 'darwin',
347+
isInstalled: false,
348+
latestVersion: '0.14.1',
349+
isBeta: false,
350+
isLatest: false
351+
};
352+
const statusBar = vscode.window.createStatusBarItem(
353+
vscode.StatusBarAlignment.Right,
354+
100
355+
);
356+
const updatedStatusBar = handleStatusBarUpdate(statusBar, devProxyInstall);
357+
358+
const expected = '$(error) Dev Proxy';
359+
const actual = updatedStatusBar.text;
360+
assert.strictEqual(actual, expected);
361+
});
362+
363+
test('should show warning statusbar when devproxy is not latest version', async () => {
364+
const devProxyInstall: DevProxyInstall = {
365+
filePath: 'somepath/devproxy',
366+
version: '0.1.0',
367+
platform: 'win32',
368+
isInstalled: true,
369+
latestVersion: '0.14.1',
370+
isBeta: false,
371+
isLatest: false
372+
};
373+
const statusBar = vscode.window.createStatusBarItem(
374+
vscode.StatusBarAlignment.Right,
375+
100
376+
);
377+
const updatedStatusBar = handleStatusBarUpdate(statusBar, devProxyInstall);
378+
379+
const expected = '$(warning) Dev Proxy 0.1.0';
380+
const actual = updatedStatusBar.text;
381+
assert.strictEqual(actual, expected);
382+
});
383+
384+
test('should show success statusbar when devproxy is installed and latest version', async () => {
385+
const devProxyInstall: DevProxyInstall = {
386+
filePath: 'somepath/devproxy',
387+
version: '0.14.1',
388+
platform: 'win32',
389+
isInstalled: true,
390+
latestVersion: '0.14.1',
391+
isBeta: false,
392+
isLatest: true
393+
};
394+
const statusBar = vscode.window.createStatusBarItem(
395+
vscode.StatusBarAlignment.Right,
396+
100
397+
);
398+
const updatedStatusBar = handleStatusBarUpdate(statusBar, devProxyInstall);
399+
400+
const expected = '$(check) Dev Proxy 0.14.1';
401+
const actual = updatedStatusBar.text;
402+
assert.strictEqual(actual, expected);
403+
});
338404
});

0 commit comments

Comments
 (0)