Open History to see the last commit
1interface ParsedCliSemver {
2 readonly major: number;
3 readonly minor: number;
4 readonly patch: number;
5 readonly prerelease: ReadonlyArray<string>;
6}
7
8const CLI_VERSION_NUMBER_SEGMENT = /^\d+$/;
9
10export function normalizeCliVersion(version: string): string {
11 const [main, prerelease] = version.trim().split("-", 2);
12 const segments = (main ?? "")
13 .split(".")
14 .map((segment) => segment.trim())
15 .filter((segment) => segment.length > 0);
16
17 if (segments.length === 2) {
18 segments.push("0");
19 }
20
21 return prerelease ? `${segments.join(".")}-${prerelease}` : segments.join(".");
22}
23
24function parseCliSemver(version: string): ParsedCliSemver | null {
25 const normalized = normalizeCliVersion(version);
26 const [main = "", prerelease] = normalized.split("-", 2);
27 const segments = main.split(".");
28 if (segments.length !== 3) {
29 return null;
30 }
31
32 const [majorSegment, minorSegment, patchSegment] = segments;
33 if (majorSegment === undefined || minorSegment === undefined || patchSegment === undefined) {
34 return null;
35 }
36 if (
37 !CLI_VERSION_NUMBER_SEGMENT.test(majorSegment) ||
38 !CLI_VERSION_NUMBER_SEGMENT.test(minorSegment) ||
39 !CLI_VERSION_NUMBER_SEGMENT.test(patchSegment)
40 ) {
41 return null;
42 }
43
44 const major = Number.parseInt(majorSegment, 10);
45 const minor = Number.parseInt(minorSegment, 10);
46 const patch = Number.parseInt(patchSegment, 10);
47 if (![major, minor, patch].every(Number.isInteger)) {
48 return null;
49 }
50
51 return {
52 major,
53 minor,
54 patch,
55 prerelease:
56 prerelease
57 ?.split(".")
58 .map((segment) => segment.trim())
59 .filter((segment) => segment.length > 0) ?? [],
60 };
61}
62
63function comparePrereleaseIdentifier(left: string, right: string): number {
64 const leftNumeric = /^\d+$/.test(left);
65 const rightNumeric = /^\d+$/.test(right);
66
67 if (leftNumeric && rightNumeric) {
68 return Number.parseInt(left, 10) - Number.parseInt(right, 10);
69 }
70 if (leftNumeric) {
71 return -1;
72 }
73 if (rightNumeric) {
74 return 1;
75 }
76 return left.localeCompare(right);
77}
78
79export function compareCliVersions(left: string, right: string): number {
80 const parsedLeft = parseCliSemver(left);
81 const parsedRight = parseCliSemver(right);
82 if (!parsedLeft || !parsedRight) {
83 return left.localeCompare(right);
84 }
85
86 if (parsedLeft.major !== parsedRight.major) {
87 return parsedLeft.major - parsedRight.major;
88 }
89 if (parsedLeft.minor !== parsedRight.minor) {
90 return parsedLeft.minor - parsedRight.minor;
91 }
92 if (parsedLeft.patch !== parsedRight.patch) {
93 return parsedLeft.patch - parsedRight.patch;
94 }
95
96 if (parsedLeft.prerelease.length === 0 && parsedRight.prerelease.length === 0) {
97 return 0;
98 }
99 if (parsedLeft.prerelease.length === 0) {
100 return 1;
101 }
102 if (parsedRight.prerelease.length === 0) {
103 return -1;
104 }
105
106 const length = Math.max(parsedLeft.prerelease.length, parsedRight.prerelease.length);
107 for (let index = 0; index < length; index += 1) {
108 const leftIdentifier = parsedLeft.prerelease[index];
109 const rightIdentifier = parsedRight.prerelease[index];
110 if (leftIdentifier === undefined) {
111 return -1;
112 }
113 if (rightIdentifier === undefined) {
114 return 1;
115 }
116 const comparison = comparePrereleaseIdentifier(leftIdentifier, rightIdentifier);
117 if (comparison !== 0) {
118 return comparison;
119 }
120 }
121
122 return 0;
123}