Skip to content

Commit 21ff236

Browse files
authored
Adds beta support. Closes #51 (#64)
1 parent 50f0864 commit 21ff236

File tree

7 files changed

+92
-127
lines changed

7 files changed

+92
-127
lines changed

src/constants.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,4 @@ export const pluginDocs: PluginDocs = {
195195
name: 'Retry After Plugin',
196196
url: 'https://learn.microsoft.com/microsoft-cloud/dev/dev-proxy/technical-reference/retryafterplugin',
197197
}
198-
};
199-
200-
export const testDevProxyInstall: DevProxyInstall = {
201-
filePath: 'somepath/devproxy',
202-
isBeta: false,
203-
isInstalled: true,
204-
isLatest: true,
205-
latestVersion: '0.14.1',
206-
platform: 'win32',
207-
version: '0.14.1',
208-
isRunning: false
209-
};
198+
};

src/detect.ts

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,12 @@
11
import { exec } from 'child_process';
2-
import { DevProxyInstall, Release } from './types';
2+
import { DevProxyInstall } from './types';
33
import os from 'os';
44

5-
const getExecutablePath = async (filename: string): Promise<string> => {
6-
const command = getFindCommand();
7-
if (command === '') {
8-
return '';
9-
}
10-
5+
export const getVersion = async () => {
116
try {
12-
return await executeCommand(`${command} ${filename}`);
13-
} catch (error) {
14-
console.error(error);
15-
return '';
16-
}
17-
};
18-
19-
const getFindCommand = () => {
20-
const platform = os.platform();
21-
let command = '';
22-
if (platform === 'win32') {
23-
command = 'pwsh.exe -c "where.exe devproxy"';
24-
}
25-
if (platform === 'darwin') {
26-
command = '$SHELL -c "which devproxy"';
27-
}
28-
return command;
29-
};
30-
31-
const getVersion = async (filePath: string) => {
32-
if (filePath === '') {
33-
return '';
34-
}
35-
try {
36-
const version = await executeCommand(`${filePath.trim()} --version`);
7+
const version = await executeCommand(`devproxy --version`);
378
return version.trim();
389
} catch (error) {
39-
console.error(error);
4010
return "";
4111
}
4212
};
@@ -56,31 +26,32 @@ export const executeCommand = async (cmd: string): Promise<string> => {
5626
};
5727

5828
export const detectDevProxyInstall = async (): Promise<DevProxyInstall> => {
59-
const filePath = await getExecutablePath('devproxy');
60-
const version = await getVersion(filePath);
61-
const isInstalled = filePath !== '';
29+
const version = await getVersion();
30+
const isInstalled = version !== '';
6231
const isBeta = version.includes('beta');
6332
const platform = os.platform();
64-
const latestVersion = await getLatestVersion();
65-
const isLatest = latestVersion === version;
33+
const outdatedVersion = await getOutdatedVersion();
34+
const isOutdated = isInstalled && outdatedVersion !== '';
6635
const isRunning = await isDevProxyRunning();
6736

6837
return {
69-
filePath,
7038
version,
7139
isInstalled,
7240
isBeta,
7341
platform,
74-
latestVersion,
75-
isLatest,
42+
outdatedVersion,
43+
isOutdated,
7644
isRunning
7745
};
7846
};
7947

80-
export const getLatestVersion = async (): Promise<string> => {
81-
const request = await fetch('https://api.github.com/repos/microsoft/dev-proxy/releases/latest');
82-
const release = await request.json() as Release;
83-
return release.tag_name.replace('v', '');
48+
export const getOutdatedVersion = async (): Promise<string> => {
49+
try {
50+
const outdated = await executeCommand(`devproxy outdated --short`);
51+
return outdated ? outdated.trim() : '';
52+
} catch (error) {
53+
return "";
54+
}
8455
};
8556

8657
export const isDevProxyRunning = async (): Promise<boolean> => {

src/notifications.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ export const handleStartNotification = (context: vscode.ExtensionContext) => {
3131
};
3232
};
3333
};
34-
35-
if (!devProxyInstall.isLatest) {
34+
if (devProxyInstall.isOutdated) {
3635
return () => {
37-
const message = `New Dev Proxy version ${devProxyInstall.latestVersion} is available.`;
36+
const message = `New Dev Proxy version ${devProxyInstall.outdatedVersion} is available.`;
3837
return {
3938
message,
4039
show: async () => {

src/state.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import * as vscode from 'vscode';
22
import { detectDevProxyInstall } from './detect';
3-
import { testDevProxyInstall } from './constants';
43

54
export const updateGlobalState = async (context: vscode.ExtensionContext) => {
6-
context.extensionMode === vscode.ExtensionMode.Production
7-
? context.globalState.update('devProxyInstall', await detectDevProxyInstall())
8-
: context.globalState.update('devProxyInstall', testDevProxyInstall);
5+
context.globalState.update('devProxyInstall', await detectDevProxyInstall());
96
};

src/statusbar.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ export const updateStatusBar = (context: vscode.ExtensionContext, statusBar: vsc
1717
export const handleStatusBarUpdate = (context: vscode.ExtensionContext, statusBar: vscode.StatusBarItem): vscode.StatusBarItem => {
1818
const devProxyInstall = context.globalState.get<DevProxyInstall>('devProxyInstall');
1919
if (!devProxyInstall) { return statusBar; }
20-
if (devProxyInstall.isInstalled) {
21-
statusBar.backgroundColor = new vscode.ThemeColor('statusBar.background');
22-
statusBar.color = new vscode.ThemeColor('statusBar.foreground');
23-
statusBar.text = `$(check) Dev Proxy ${devProxyInstall.version}`;
24-
statusBar.tooltip = `You have the latest version installed`;
25-
}
2620
if (!devProxyInstall.isInstalled) {
2721
statusBar.backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground');
2822
statusBar.color = new vscode.ThemeColor('statusBarItem.errorForeground');
2923
statusBar.text = `$(error) Dev Proxy`;
3024
statusBar.tooltip = `Dev Proxy is not installed`;
3125
}
32-
if (devProxyInstall.isInstalled && !devProxyInstall.isLatest) {
26+
if (devProxyInstall.isInstalled && !devProxyInstall.isOutdated) {
27+
statusBar.backgroundColor = new vscode.ThemeColor('statusBar.background');
28+
statusBar.color = new vscode.ThemeColor('statusBar.foreground');
29+
statusBar.text = `$(check) Dev Proxy ${devProxyInstall.version}`;
30+
statusBar.tooltip = `You have the latest version installed`;
31+
}
32+
if (devProxyInstall.isInstalled && devProxyInstall.isOutdated) {
3333
statusBar.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
3434
statusBar.color = new vscode.ThemeColor('statusBarItem.warningForeground');
3535
statusBar.text = `$(warning) Dev Proxy ${devProxyInstall.version}`;

src/test/extension.test.ts

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,19 @@ import parse from 'json-to-ast';
1212
import { createCodeLensForPluginNodes } from '../codelens';
1313
import { handleStartNotification } from '../notifications';
1414
import { handleStatusBarUpdate, statusBarLoop } from '../statusbar';
15-
import { testDevProxyInstall } from '../constants';
1615
import * as sinon from 'sinon';
1716
import * as detect from '../detect';
17+
import { DevProxyInstall } from '../types';
18+
19+
export const testDevProxyInstall: DevProxyInstall = {
20+
isBeta: false,
21+
isInstalled: true,
22+
isOutdated: true,
23+
isRunning: false,
24+
outdatedVersion: '0.14.1',
25+
platform: 'win32',
26+
version: '0.14.1',
27+
};
1828

1929
suite('extension', () => {
2030

@@ -300,14 +310,14 @@ suite('notifications', () => {
300310
test('should show install notification when devproxy is not installed on mac', async () => {
301311
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
302312
await context.globalState.update('devProxyInstall', {
303-
filePath: '',
304-
version: '',
305-
platform: 'darwin',
306-
isInstalled: false,
307-
latestVersion: '0.14.1',
308313
isBeta: false,
309-
isLatest: false
310-
});
314+
isInstalled: false,
315+
isOutdated: false,
316+
isRunning: false,
317+
platform: 'darwin',
318+
outdatedVersion: '0.14.1',
319+
version: '',
320+
} as DevProxyInstall);
311321

312322
const notification = handleStartNotification(context);
313323

@@ -395,14 +405,14 @@ suite('notifications', () => {
395405
test('should show upgrade notification when devproxy is not latest version', async () => {
396406
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
397407
await context.globalState.update('devProxyInstall', {
398-
filePath: 'somepath/devproxy',
399-
version: '0.1.0',
400-
platform: 'win32',
401-
isInstalled: true,
402-
latestVersion: '0.14.1',
403408
isBeta: false,
404-
isLatest: false
405-
});
409+
isInstalled: true,
410+
isOutdated: true,
411+
isRunning: false,
412+
platform: 'win32',
413+
outdatedVersion: '0.14.1',
414+
version: '0.1.0',
415+
} as DevProxyInstall);
406416

407417
const notification = handleStartNotification(context);
408418

@@ -427,14 +437,14 @@ suite('statusbar', () => {
427437
test('should show error statusbar when devproxy is not installed', async () => {
428438
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
429439
await context.globalState.update('devProxyInstall', {
430-
filePath: '',
431-
version: '',
432-
platform: 'darwin',
433-
isInstalled: false,
434-
latestVersion: '0.14.1',
435440
isBeta: false,
436-
isLatest: false
437-
});
441+
isInstalled: false,
442+
isOutdated: false,
443+
isRunning: false,
444+
platform: 'win32',
445+
outdatedVersion: '0.14.1',
446+
version: '0.1.0',
447+
} as DevProxyInstall);
438448
const statusBar = vscode.window.createStatusBarItem(
439449
vscode.StatusBarAlignment.Right,
440450
100
@@ -449,14 +459,14 @@ suite('statusbar', () => {
449459
test('should show warning statusbar when devproxy is not latest version', async () => {
450460
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
451461
await context.globalState.update('devProxyInstall', {
452-
filePath: 'somepath/devproxy',
453-
version: '0.1.0',
454-
platform: 'win32',
455-
isInstalled: true,
456-
latestVersion: '0.14.1',
457462
isBeta: false,
458-
isLatest: false
459-
});
463+
isInstalled: true,
464+
isOutdated: true,
465+
isRunning: false,
466+
platform: 'win32',
467+
outdatedVersion: '0.14.1',
468+
version: '0.1.0',
469+
} as DevProxyInstall);
460470
const statusBar = vscode.window.createStatusBarItem(
461471
vscode.StatusBarAlignment.Right,
462472
100
@@ -471,14 +481,14 @@ suite('statusbar', () => {
471481
test('should show success statusbar when devproxy is installed and latest version', async () => {
472482
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
473483
await context.globalState.update('devProxyInstall', {
474-
filePath: 'somepath/devproxy',
475-
version: '0.14.1',
476-
platform: 'win32',
477-
isInstalled: true,
478-
latestVersion: '0.14.1',
479484
isBeta: false,
480-
isLatest: true
481-
});
485+
isInstalled: true,
486+
isOutdated: false,
487+
isRunning: false,
488+
platform: 'win32',
489+
outdatedVersion: '',
490+
version: '0.14.1',
491+
} as DevProxyInstall);
482492
const statusBar = vscode.window.createStatusBarItem(
483493
vscode.StatusBarAlignment.Right,
484494
100
@@ -521,14 +531,14 @@ suite('schema', () => {
521531
test('should show warning when $schema property does not match installed version', async () => {
522532
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
523533
await context.globalState.update('devProxyInstall', {
524-
filePath: 'somepath/devproxy',
525-
version: '0.1.0',
526-
platform: 'win32',
527-
isInstalled: true,
528-
latestVersion: '0.14.1',
529534
isBeta: false,
530-
isLatest: false
531-
});
535+
isInstalled: true,
536+
isOutdated: true,
537+
isRunning: false,
538+
platform: 'win32',
539+
outdatedVersion: '0.14.1',
540+
version: '0.1.0',
541+
} as DevProxyInstall);
532542

533543
const fileName = 'config-schema-mismatch.json';
534544
const filePath = path.resolve(__dirname, 'examples', fileName);
@@ -550,14 +560,14 @@ suite('schema', () => {
550560
test('should not show warning when $schema property matches installed version', async () => {
551561
const context = await vscode.extensions.getExtension('garrytrinder.dev-proxy-toolkit')?.activate() as vscode.ExtensionContext;
552562
await context.globalState.update('devProxyInstall', {
553-
filePath: 'somepath/devproxy',
554-
version: '0.14.1',
555-
platform: 'win32',
556-
isInstalled: true,
557-
latestVersion: '0.14.1',
558563
isBeta: false,
559-
isLatest: true
560-
});
564+
isInstalled: true,
565+
isOutdated: false,
566+
isRunning: false,
567+
platform: 'win32',
568+
outdatedVersion: '',
569+
version: '0.14.1',
570+
} as DevProxyInstall);
561571

562572
const fileName = 'config-schema-version.json';
563573
const filePath = path.resolve(__dirname, 'examples', fileName);

src/types.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ export type PluginSnippets = {
1818
};
1919

2020
export type DevProxyInstall = {
21-
filePath: string;
22-
version: string;
23-
isInstalled: boolean;
2421
isBeta: boolean;
25-
platform: NodeJS.Platform;
26-
latestVersion: string;
27-
isLatest: boolean;
22+
isInstalled: boolean;
23+
isOutdated: boolean;
2824
isRunning: boolean;
25+
outdatedVersion: string;
26+
platform: NodeJS.Platform;
27+
version: string;
2928
};
3029

3130
export type Release = {

0 commit comments

Comments
 (0)