Open History to see the last commit
1import * as Option from "effect/Option";
2import * as Schema from "effect/Schema";
3
4import { compareSemverVersions } from "./semver.ts";
5
6const CLI_RELEASE_REPOSITORY = "pingdotgg/t3code";
7export const CLI_RELEASES_PAGE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases`;
8export const CLI_RELEASE_CHECKSUMS_FILE = "SHA256SUMS";
9export const CLI_RELEASE_BASE_URL_ENV = "T3CODE_RELEASE_BASE_URL";
10
11// Keep archive keys aligned with the Linux matrix, Windows ARM64 build, and cli_archive rows in release.yml.
12// Node single-executables do not support darwin-x64 and segfault at startup.
13const CLI_ARCHIVE_PLATFORM_KEYS = [
14 "darwin-arm64",
15 "linux-arm64",
16 "linux-x64",
17 "win32-arm64",
18 "win32-x64",
19] as const;
20export type CliArchivePlatformKey = (typeof CLI_ARCHIVE_PLATFORM_KEYS)[number];
21
22export function cliArchivePlatformKey(
23 platform: NodeJS.Platform,
24 arch: string,
25): CliArchivePlatformKey | undefined {
26 const key = `${platform}-${arch}`;
27 return CLI_ARCHIVE_PLATFORM_KEYS.find((candidate) => candidate === key);
28}
29
30/** Use System32 bsdtar on Windows; Git-for-Windows GNU tar cannot open zip archives. */
31export function cliArchiveTarCommand(
32 platform: NodeJS.Platform,
33 env: Readonly<Record<string, string | undefined>>,
34): string {
35 if (platform !== "win32") return "tar";
36 const systemRoot = env["SystemRoot"] ?? env["windir"] ?? "C:\\Windows";
37 return `${systemRoot}\\System32\\tar.exe`;
38}
39
40export function cliArchiveFileName(version: string, platformKey: CliArchivePlatformKey): string {
41 return `t3-${version}-${platformKey}.${platformKey.startsWith("win32") ? "zip" : "tar.gz"}`;
42}
43
44const CLI_RELEASE_DEFAULT_BASE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases/download`;
45
46export function cliReleaseDownloadBaseUrl(
47 version: string,
48 baseUrl: string | undefined = CLI_RELEASE_DEFAULT_BASE_URL,
49): string {
50 return `${(baseUrl?.trim() || CLI_RELEASE_DEFAULT_BASE_URL).replace(/\/+$/, "")}/v${version}`;
51}
52
53/** `segments` added to `baseUrl`'s path, keeping its query, such as a signed mirror URL's token. */
54export function releaseAssetUrl(baseUrl: string, ...segments: ReadonlyArray<string>): string {
55 const url = new URL(baseUrl);
56 url.pathname = [url.pathname.replace(/\/+$/, ""), ...segments.map(encodeURIComponent)].join("/");
57 return url.toString();
58}
59
60/** A file of release `version`, from `baseUrl` laid out as GitHub's release downloads. */
61export function cliReleaseAssetUrl(
62 version: string,
63 fileName: string,
64 baseUrl: string | undefined = CLI_RELEASE_DEFAULT_BASE_URL,
65): string {
66 return releaseAssetUrl(baseUrl?.trim() || CLI_RELEASE_DEFAULT_BASE_URL, `v${version}`, fileName);
67}
68
69export function parseChecksums(text: string): ReadonlyMap<string, string> {
70 const checksums = new Map<string, string>();
71 for (const line of text.split(/\r?\n/)) {
72 const match = /^([0-9a-fA-F]{64})\s+\*?(\S.*)$/.exec(line.trim());
73 if (match?.[1] !== undefined && match[2] !== undefined) {
74 checksums.set(match[2], match[1].toLowerCase());
75 }
76 }
77 return checksums;
78}
79
80export type CliReleaseChannel = "stable" | "nightly" | "preview";
81export const CLI_RELEASE_CHANNELS: ReadonlyArray<CliReleaseChannel> = [
82 "stable",
83 "nightly",
84 "preview",
85];
86
87export function cliReleaseChannelOf(version: string): CliReleaseChannel {
88 const channel = /^[^-+]+-(nightly|preview)\.\d{8}\.\d+$/.exec(version)?.[1];
89 return channel === "nightly" || channel === "preview" ? channel : "stable";
90}
91
92/** Whether updating between these versions crosses Stable and Nightly. Preview builds cross nothing.
93 * Hosts refuse a crossing outside a track switch, which checks the database first. */
94export function crossesReleaseTrack(fromVersion: string, toVersion: string): boolean {
95 const from = cliReleaseChannelOf(fromVersion);
96 const to = cliReleaseChannelOf(toVersion);
97 return from !== to && from !== "preview" && to !== "preview";
98}
99
100/** The desktop update track a build belongs to. Preview builds have no feed and read as Stable. */
101export function desktopUpdateChannelOf(version: string): "latest" | "nightly" {
102 return cliReleaseChannelOf(version) === "nightly" ? "nightly" : "latest";
103}
104
105/**
106 * The update feed file a desktop app on this platform reads, which an update mirror publishes at
107 * its root beside the release's CLI archives.
108 */
109export function updateFeedFileName(
110 channel: "latest" | "nightly",
111 platform: NodeJS.Platform,
112 arch: string,
113): string {
114 if (platform === "darwin") return `${channel}-mac.yml`;
115 if (platform === "linux")
116 return arch === "arm64" ? `${channel}-linux-arm64.yml` : `${channel}-linux.yml`;
117 return `${channel}.yml`;
118}
119
120/** Whether a track switch may install `found` after its database check cleared `checked`: any build
121 * on the switch's track at least as new, since later releases only add migrations. */
122export function acceptsTrackSwitchRelease(
123 found: string,
124 channel: "latest" | "nightly",
125 checked: string,
126): boolean {
127 return (
128 cliReleaseChannelOf(found) === (channel === "nightly" ? "nightly" : "stable") &&
129 compareSemverVersions(found, checked) >= 0
130 );
131}
132
133/** Page until the channel matches; nightlies can push preview/stable releases past the first page. */
134export function cliReleaseIndexPageUrl(page: number): string {
135 return `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=100&page=${page}`;
136}
137
138/** Draft assets are not downloadable. */
139export function newestCliReleaseVersion(
140 releases: ReadonlyArray<{
141 readonly tag_name: string;
142 readonly draft?: boolean | undefined;
143 }>,
144 channel: CliReleaseChannel,
145): string | undefined {
146 for (const release of releases) {
147 if (release.draft) continue;
148 const version = /^v(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/.exec(release.tag_name)?.[1];
149 if (version === undefined) continue;
150 if (cliReleaseChannelOf(version) === channel) return version;
151 }
152 return undefined;
153}
154
155const NpmReleaseMigrationManifest = Schema.Struct({
156 t3code: Schema.Struct({ databaseMigration: Schema.Int }),
157});
158const decodeNpmReleaseMigrationManifest = Schema.decodeUnknownOption(NpmReleaseMigrationManifest);
159
160/** Highest database migration a release's server knows, from its npm `t3` manifest. */
161export function npmReleaseDatabaseMigration(manifest: unknown): number | undefined {
162 return Option.getOrUndefined(decodeNpmReleaseMigrationManifest(manifest))?.t3code
163 .databaseMigration;
164}