-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdetect.ts
More file actions
115 lines (101 loc) · 4.28 KB
/
detect.ts
File metadata and controls
115 lines (101 loc) · 4.28 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
import { DevProxyInstall } from './types';
import os from 'os';
import { VersionExeName, VersionPreference } from './enums';
import { executeCommand, resolveDevProxyExecutable } from './utils/shell';
import * as vscode from 'vscode';
export const getVersion = async (devProxyExe: string) => {
try {
const version = await executeCommand(`${devProxyExe} --version`);
const versionLines = version.trim().split('\n');
const lastLine = versionLines[versionLines.length - 1];
return lastLine.trim();
} catch (error) {
return "";
}
};
export const detectDevProxyInstall = async (versionPreference: VersionPreference): Promise<DevProxyInstall> => {
const configuration = vscode.workspace.getConfiguration('dev-proxy-toolkit');
const customPath = configuration.get<string>('devProxyPath');
const exeName = getDevProxyExe(versionPreference);
const devProxyExe = await resolveDevProxyExecutable(exeName, customPath);
const version = await getVersion(devProxyExe);
const isInstalled = version !== '';
const isBeta = version.includes('beta');
const platform = os.platform();
const outdatedVersion = await getOutdatedVersion(devProxyExe);
const isOutdated = isInstalled && outdatedVersion !== '';
const isRunning = await isDevProxyRunning(devProxyExe);
vscode.commands.executeCommand('setContext', 'isDevProxyRunning', isRunning);
return {
version,
isInstalled,
isBeta,
platform,
outdatedVersion,
isOutdated,
isRunning
};
};
export const extractVersionFromOutput = (output: string): string => {
if (!output) {
return '';
}
// Split into lines and look for version information on dedicated lines
// This avoids extracting versions from file paths like /opt/homebrew/Cellar/dev-proxy/v0.29.1/devproxy-errors.json
const lines = output.split('\n');
// Look for lines that contain version information (not file paths)
for (const line of lines) {
const trimmedLine = line.trim();
// Skip lines that contain file paths (indicated by slashes and common path patterns)
if (trimmedLine.includes('/') || trimmedLine.includes('\\') || trimmedLine.includes('loaded from')) {
continue;
}
// Look for version pattern on non-filepath lines
// Matches: major.minor.patch[-prerelease][+build] but only captures up to prerelease
const semverRegex = /v?(\d+\.\d+\.\d+(?:-[a-zA-Z0-9.-]+)?)(?:\+[a-zA-Z0-9.-]+)?/;
const match = trimmedLine.match(semverRegex);
if (match) {
return match[1];
}
}
return '';
};
export const getOutdatedVersion = async (devProxyExe: string): Promise<string> => {
try {
const outdated = await executeCommand(`${devProxyExe} outdated --short`);
return extractVersionFromOutput(outdated);
} catch (error) {
return "";
}
};
export const isDevProxyRunning = async (devProxyExe: string): Promise<boolean> => {
try {
// Get the API port from configuration
const configuration = vscode.workspace.getConfiguration('dev-proxy-toolkit');
const apiPort = configuration.get('apiPort') as number;
// Try to connect to the Dev Proxy API on the configured port
const response = await fetch(`http://127.0.0.1:${apiPort}/proxy`, {
method: 'GET',
signal: AbortSignal.timeout(2000), // 2 second timeout
});
// If we get any response (even an error), Dev Proxy is running
return response.status >= 200 && response.status < 500;
} catch (error) {
// If the request fails (connection refused, timeout, etc.), Dev Proxy is not running
return false;
}
};
export const getDevProxyExe = (versionPreference: VersionPreference) => {
return versionPreference === VersionPreference.Stable
? VersionExeName.Stable
: VersionExeName.Beta;
};
/**
* Get the normalized Dev Proxy version from an install object.
* Strips the beta pre-release suffix for version comparison.
*/
export const getNormalizedVersion = (devProxyInstall: DevProxyInstall): string => {
return devProxyInstall.isBeta
? devProxyInstall.version.split('-')[0]
: devProxyInstall.version;
};