forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.ts
More file actions
96 lines (80 loc) · 3.02 KB
/
Copy pathaction.ts
File metadata and controls
96 lines (80 loc) · 3.02 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
import { PACKAGE_VERSION, getDistroConfig } from '../../constants';
import { execSync } from 'child_process';
const distroConfig = getDistroConfig();
export async function fetchLatestVersion(): Promise<string> {
const registryUrl = `${distroConfig.registryUrl}/${distroConfig.packageName}/latest`;
const response = await fetch(registryUrl);
if (!response.ok) {
throw new Error(`Failed to fetch latest version: ${response.statusText}`);
}
const data = (await response.json()) as { version: string };
return data.version;
}
export function compareVersions(current: string, latest: string): number {
const parse = (v: string) => {
const [core = '', ...prereleaseParts] = v.split('-');
const nums = core.split('.').map(Number);
const prerelease = prereleaseParts.join('-');
return { nums, prerelease };
};
const curr = parse(current);
const lat = parse(latest);
// Compare major.minor.patch
for (let i = 0; i < 3; i++) {
const c = curr.nums[i] ?? 0;
const l = lat.nums[i] ?? 0;
if (l > c) return 1;
if (l < c) return -1;
}
// Equal core versions — compare pre-release segments
if (!curr.prerelease && !lat.prerelease) return 0;
// A version without pre-release is greater than one with (1.0.0 > 1.0.0-preview)
if (!curr.prerelease) return -1;
if (!lat.prerelease) return 1;
const currSegments = curr.prerelease.split('.');
const latSegments = lat.prerelease.split('.');
const len = Math.max(currSegments.length, latSegments.length);
for (let i = 0; i < len; i++) {
const cs = currSegments[i];
const ls = latSegments[i];
if (cs === undefined) return 1; // fewer segments = earlier
if (ls === undefined) return -1;
const cn = Number(cs);
const ln = Number(ls);
// Both numeric — compare numerically
if (!isNaN(cn) && !isNaN(ln)) {
if (ln > cn) return 1;
if (ln < cn) return -1;
} else {
// Lexicographic comparison for non-numeric segments
if (ls > cs) return 1;
if (ls < cs) return -1;
}
}
return 0;
}
export type UpdateStatus = 'up-to-date' | 'newer-local' | 'update-available' | 'updated' | 'update-failed';
export interface UpdateResult {
status: UpdateStatus;
currentVersion: string;
latestVersion: string;
}
export async function handleUpdate(checkOnly: boolean): Promise<UpdateResult> {
const latestVersion = await fetchLatestVersion();
const comparison = compareVersions(PACKAGE_VERSION, latestVersion);
if (comparison === 0) {
return { status: 'up-to-date', currentVersion: PACKAGE_VERSION, latestVersion };
}
if (comparison < 0) {
return { status: 'newer-local', currentVersion: PACKAGE_VERSION, latestVersion };
}
if (checkOnly) {
return { status: 'update-available', currentVersion: PACKAGE_VERSION, latestVersion };
}
try {
execSync(distroConfig.installCommand, { stdio: 'inherit' });
return { status: 'updated', currentVersion: PACKAGE_VERSION, latestVersion };
} catch {
return { status: 'update-failed', currentVersion: PACKAGE_VERSION, latestVersion };
}
}