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
53export function parseChecksums(text: string): ReadonlyMap<string, string> {
54 const checksums = new Map<string, string>();
55 for (const line of text.split(/\r?\n/)) {
56 const match = /^([0-9a-fA-F]{64})\s+\*?(\S.*)$/.exec(line.trim());
57 if (match?.[1] !== undefined && match[2] !== undefined) {
58 checksums.set(match[2], match[1].toLowerCase());
59 }
60 }
61 return checksums;
62}
63
64export type CliReleaseChannel = "stable" | "nightly" | "preview";
65export const CLI_RELEASE_CHANNELS: ReadonlyArray<CliReleaseChannel> = [
66 "stable",
67 "nightly",
68 "preview",
69];
70
71export function cliReleaseChannelOf(version: string): CliReleaseChannel {
72 const channel = /^[^-+]+-(nightly|preview)\.\d{8}\.\d+$/.exec(version)?.[1];
73 return channel === "nightly" || channel === "preview" ? channel : "stable";
74}
75
76/** Whether updating between these versions crosses Stable and Nightly. Preview builds cross nothing.
77 * Hosts refuse a crossing outside a track switch, which checks the database first. */
78export function crossesReleaseTrack(fromVersion: string, toVersion: string): boolean {
79 const from = cliReleaseChannelOf(fromVersion);
80 const to = cliReleaseChannelOf(toVersion);
81 return from !== to && from !== "preview" && to !== "preview";
82}
83
84/** The desktop update track a build belongs to. Preview builds have no feed and read as Stable. */
85export function desktopUpdateChannelOf(version: string): "latest" | "nightly" {
86 return cliReleaseChannelOf(version) === "nightly" ? "nightly" : "latest";
87}
88
89/** Whether a track switch may install `found` after its database check cleared `checked`: any build
90 * on the switch's track at least as new, since later releases only add migrations. */
91export function acceptsTrackSwitchRelease(
92 found: string,
93 channel: "latest" | "nightly",
94 checked: string,
95): boolean {
96 return (
97 cliReleaseChannelOf(found) === (channel === "nightly" ? "nightly" : "stable") &&
98 compareSemverVersions(found, checked) >= 0
99 );
100}
101
102/** Page until the channel matches; nightlies can push preview/stable releases past the first page. */
103export function cliReleaseIndexPageUrl(page: number): string {
104 return `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=100&page=${page}`;
105}
106
107/** Draft assets are not downloadable. */
108export function newestCliReleaseVersion(
109 releases: ReadonlyArray<{
110 readonly tag_name: string;
111 readonly draft?: boolean | undefined;
112 }>,
113 channel: CliReleaseChannel,
114): string | undefined {
115 for (const release of releases) {
116 if (release.draft) continue;
117 const version = /^v(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/.exec(release.tag_name)?.[1];
118 if (version === undefined) continue;
119 if (cliReleaseChannelOf(version) === channel) return version;
120 }
121 return undefined;
122}
123
124const NpmReleaseMigrationManifest = Schema.Struct({
125 t3code: Schema.Struct({ databaseMigration: Schema.Int }),
126});
127const decodeNpmReleaseMigrationManifest = Schema.decodeUnknownOption(NpmReleaseMigrationManifest);
128
129/** Highest database migration a release's server knows, from its npm `t3` manifest. */
130export function npmReleaseDatabaseMigration(manifest: unknown): number | undefined {
131 return Option.getOrUndefined(decodeNpmReleaseMigrationManifest(manifest))?.t3code
132 .databaseMigration;
133}