-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathversion.ts
More file actions
267 lines (230 loc) · 9.24 KB
/
Copy pathversion.ts
File metadata and controls
267 lines (230 loc) · 9.24 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import * as fs from 'fs';
import * as path from 'path';
import fetch from 'node-fetch';
import chalk from 'chalk';
export interface VersionInfo {
version: string;
buildTime: string;
source: string;
}
export interface VersionCheckResult {
currentVersion: string;
latestVersion: string;
isUpdateAvailable: boolean;
updateMessage?: string;
}
// This will be replaced by the build script
let versionData: VersionInfo = {
version: '0.5.1',
buildTime: '2025-08-16T07:56:39.441Z',
source: 'release'
};
// Try to read from package.json as fallback for development
try {
const packageJsonPath = path.join(__dirname, '../../package.json');
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
versionData = {
version: packageJson.version || '0.1.0',
buildTime: new Date().toISOString(),
source: 'package.json'
};
}
} catch {
// Use static fallback if package.json can't be read
}
export const getGatewayVersion = (): VersionInfo => {
return {
version: versionData.version || '0.1.0',
buildTime: versionData.buildTime,
source: versionData.source
};
};
export const getVersionString = (): string => {
return getGatewayVersion().version;
};
/**
* Check if a newer version is available on npm (with optional debug output)
* @param timeout - Request timeout in milliseconds (default: 5000)
* @param debug - Show debug information about the API call
* @returns Promise<VersionCheckResult>
*/
export const checkForUpdates = async (timeout: number = 5000, debug: boolean = false): Promise<VersionCheckResult> => {
const currentVersion = getVersionString();
if (debug) {
console.log(chalk.blue('🔍 Debug: Starting version check...'));
console.log(chalk.gray(` Current version: ${currentVersion}`));
console.log(chalk.gray(` Timeout: ${timeout}ms`));
console.log(chalk.gray(` API endpoint: https://registry.npmjs.org/@deploystack/gateway`));
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
if (debug) {
console.log(chalk.blue('🌐 Debug: Fetching from npm registry...'));
}
// Use the correct npm registry API endpoint
const response = await fetch('https://registry.npmjs.org/@deploystack/gateway', {
signal: controller.signal,
headers: {
'Accept': 'application/json',
'User-Agent': `@deploystack/gateway/${currentVersion}`
}
});
clearTimeout(timeoutId);
if (debug) {
console.log(chalk.blue(`📊 Debug: Response status: ${response.status} ${response.statusText}`));
}
if (!response.ok) {
if (debug) {
console.log(chalk.red(`❌ Debug: HTTP error ${response.status}`));
}
return {
currentVersion,
latestVersion: currentVersion,
isUpdateAvailable: false,
updateMessage: `Failed to check for updates (HTTP ${response.status})`
};
}
const data = await response.json() as {
name?: string;
'dist-tags': { latest: string };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
versions: { [key: string]: any };
description?: string;
time?: { modified: string };
};
if (debug) {
console.log(chalk.blue('📊 Debug: Parsed npm response'));
console.log(chalk.gray(` Package name: ${data.name || 'N/A'}`));
console.log(chalk.gray(` Latest tag: ${data['dist-tags']?.latest || 'N/A'}`));
console.log(chalk.gray(` Available tags: ${Object.keys(data['dist-tags'] || {}).join(', ')}`));
console.log(chalk.gray(` Total versions: ${Object.keys(data.versions || {}).length}`));
}
const latestVersion = data['dist-tags'].latest;
if (!latestVersion) {
return {
currentVersion,
latestVersion: currentVersion,
isUpdateAvailable: false,
updateMessage: 'Could not determine latest version from npm'
};
}
const isUpdateAvailable = compareVersions(currentVersion, latestVersion) < 0;
if (debug) {
console.log(chalk.blue('🔍 Debug: Version comparison'));
console.log(chalk.gray(` Current: ${currentVersion}`));
console.log(chalk.gray(` Latest: ${latestVersion}`));
console.log(chalk.gray(` Update available: ${isUpdateAvailable}`));
console.log(chalk.gray(` Comparison result: ${compareVersions(currentVersion, latestVersion)}`));
}
return {
currentVersion,
latestVersion,
isUpdateAvailable,
updateMessage: isUpdateAvailable
? `Update available! Run: ${chalk.cyan('npm install -g @deploystack/gateway@latest')}`
: undefined
};
} catch (error) {
// Handle timeout or network errors gracefully
const errorMessage = error instanceof Error && error.name === 'AbortError'
? 'Request timed out'
: error instanceof Error ? error.message : 'Network error';
if (debug) {
console.log(chalk.red('❌ Debug: Error occurred'));
console.log(chalk.gray(` Error name: ${error instanceof Error ? error.name : 'Unknown'}`));
console.log(chalk.gray(` Error message: ${errorMessage}`));
if (error instanceof Error && error.stack) {
console.log(chalk.gray(` Stack trace: ${error.stack.split('\n')[0]}`));
}
}
return {
currentVersion,
latestVersion: currentVersion,
isUpdateAvailable: false,
updateMessage: `Could not check for updates (${errorMessage})`
};
}
};
/**
* Compare two semantic version strings
* @param current - Current version (e.g., "1.2.3")
* @param latest - Latest version (e.g., "1.3.0")
* @returns -1 if current < latest, 0 if equal, 1 if current > latest
*/
function compareVersions(current: string, latest: string): number {
// Remove 'v' prefix if present
const cleanCurrent = current.replace(/^v/, '');
const cleanLatest = latest.replace(/^v/, '');
// Parse version parts
const parseVersion = (version: string) => {
const [versionPart, preReleasePart] = version.split('-');
const numbers = versionPart.split('.').map(part => parseInt(part, 10) || 0);
return {
numbers,
isPreRelease: !!preReleasePart,
preRelease: preReleasePart || ''
};
};
const currentVersion = parseVersion(cleanCurrent);
const latestVersion = parseVersion(cleanLatest);
// Ensure both arrays have the same length
const maxLength = Math.max(currentVersion.numbers.length, latestVersion.numbers.length);
while (currentVersion.numbers.length < maxLength) currentVersion.numbers.push(0);
while (latestVersion.numbers.length < maxLength) latestVersion.numbers.push(0);
// Compare version numbers first
for (let i = 0; i < maxLength; i++) {
if (currentVersion.numbers[i] < latestVersion.numbers[i]) return -1;
if (currentVersion.numbers[i] > latestVersion.numbers[i]) return 1;
}
// If version numbers are equal, handle pre-release logic
// Pre-release versions are considered lower than stable versions
if (currentVersion.isPreRelease && !latestVersion.isPreRelease) {
return -1; // current is pre-release, latest is stable
}
if (!currentVersion.isPreRelease && latestVersion.isPreRelease) {
return 1; // current is stable, latest is pre-release
}
// Both are pre-release or both are stable with same version numbers
if (currentVersion.isPreRelease && latestVersion.isPreRelease) {
// Compare pre-release identifiers lexicographically
return currentVersion.preRelease.localeCompare(latestVersion.preRelease);
}
return 0;
}
/**
* Display version information with update check
* @param skipUpdateCheck - Skip the update check (useful for offline scenarios)
* @param debug - Show debug information about the API call
*/
export const displayVersionInfo = async (skipUpdateCheck: boolean = false, debug: boolean = false): Promise<void> => {
const versionInfo = getGatewayVersion();
console.log(`${chalk.bold('DeployStack Gateway')} v${chalk.green(versionInfo.version)}`);
console.log(`Built: ${chalk.gray(new Date(versionInfo.buildTime).toLocaleString())}`);
console.log(`Source: ${chalk.gray(versionInfo.source)}`);
if (!skipUpdateCheck) {
console.log(); // Empty line
console.log(chalk.gray('Checking npm registry for updates...'));
try {
const updateCheck = await checkForUpdates(5000, debug);
if (updateCheck.isUpdateAvailable) {
console.log();
console.log(chalk.yellow('📦 Update Available!'));
console.log(` Current: ${chalk.red(updateCheck.currentVersion)}`);
console.log(` Latest: ${chalk.green(updateCheck.latestVersion)}`);
console.log(` Package: ${chalk.blue('https://www.npmjs.com/package/@deploystack/gateway')}`);
if (updateCheck.updateMessage) {
console.log(` ${updateCheck.updateMessage}`);
}
} else if (updateCheck.updateMessage && !updateCheck.isUpdateAvailable) {
// Show error message if there was an issue checking
console.log(chalk.gray(`ℹ️ ${updateCheck.updateMessage}`));
} else {
console.log(chalk.green('✅ You are running the latest version'));
}
} catch {
console.log(chalk.gray('ℹ️ Could not check for updates'));
}
}
};