Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cliRelease.ts

106 lines3.8 KB

1const CLI_RELEASE_REPOSITORY = "pingdotgg/t3code";
2export const CLI_RELEASES_PAGE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases`;
3export const CLI_RELEASE_CHECKSUMS_FILE = "SHA256SUMS";
4export const CLI_RELEASE_BASE_URL_ENV = "T3CODE_RELEASE_BASE_URL";
5​
6/**
7 * Kept in step with the build_linux_cli matrix, build_windows_arm64_cli, and
8 * the `cli_archive` rows in .github/workflows/release.yml: a key here without a
9 * build there produces download URLs that 404, and a build there without a key
10 * here is unreachable from every installer.
11 */
12// No darwin-x64: Node single-executables are unsupported on x64 macOS (the
13// SEA docs list macOS as arm64 only) and the binary segfaults on start.
14const CLI_ARCHIVE_PLATFORM_KEYS = [
15 "darwin-arm64",
16 "linux-arm64",
17 "linux-x64",
18 "win32-arm64",
19 "win32-x64",
20] as const;
21export type CliArchivePlatformKey = (typeof CLI_ARCHIVE_PLATFORM_KEYS)[number];
22​
23export function cliArchivePlatformKey(
24 platform: NodeJS.Platform,
25 arch: string,
26): CliArchivePlatformKey | undefined {
27 const key = `${platform}-${arch}`;
28 return CLI_ARCHIVE_PLATFORM_KEYS.find((candidate) => candidate === key);
30​
31/**
32 * Windows ships bsdtar in System32, which reads both formats; a Git-for-Windows
33 * GNU tar earlier on PATH cannot open the zip, so the system copy is named by
34 * absolute path.
35 */
36export function cliArchiveTarCommand(
37 platform: NodeJS.Platform,
38 env: Readonly<Record<string, string | undefined>>,
39): string {
40 if (platform !== "win32") return "tar";
41 const systemRoot = env["SystemRoot"] ?? env["windir"] ?? "C:\\Windows";
42 return `${systemRoot}\\System32\\tar.exe`;
44​
45export function cliArchiveFileName(version: string, platformKey: CliArchivePlatformKey): string {
46 return `t3-${version}-${platformKey}.${platformKey.startsWith("win32") ? "zip" : "tar.gz"}`;
48​
49const CLI_RELEASE_DEFAULT_BASE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases/download`;
50​
51export function cliReleaseDownloadBaseUrl(
52 version: string,
53 baseUrl: string | undefined = CLI_RELEASE_DEFAULT_BASE_URL,
54): string {
55 return `${(baseUrl?.trim() || CLI_RELEASE_DEFAULT_BASE_URL).replace(/\/+$/, "")}/v${version}`;
57​
58export function parseChecksums(text: string): ReadonlyMap<string, string> {
59 const checksums = new Map<string, string>();
60 for (const line of text.split(/\r?\n/)) {
61 const match = /^([0-9a-fA-F]{64})\s+\*?(\S.*)$/.exec(line.trim());
62 if (match?.[1] !== undefined && match[2] !== undefined) {
63 checksums.set(match[2], match[1].toLowerCase());
64 }
65 }
66 return checksums;
68​
69export type CliReleaseChannel = "stable" | "nightly" | "preview";
70export const CLI_RELEASE_CHANNELS: ReadonlyArray<CliReleaseChannel> = [
71 "stable",
72 "nightly",
73 "preview",
74];
75​
76export function cliReleaseChannelOf(version: string): CliReleaseChannel {
77 const channel = /^[^-+]+-(nightly|preview)\.\d{8}\.\d+$/.exec(version)?.[1];
78 return channel === "nightly" || channel === "preview" ? channel : "stable";
80​
81/**
82 * Callers walk pages until a channel match turns up; a busy nightly train can push the newest
83 * preview or stable release past any single page.
84 */
85export function cliReleaseIndexPageUrl(page: number): string {
86 return `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=100&page=${page}`;
88​
89/**
90 * Drafts are skipped because their assets are not downloadable.
91 */
92export function newestCliReleaseVersion(
93 releases: ReadonlyArray<{
94 readonly tag_name: string;
95 readonly draft?: boolean | undefined;
96 }>,
97 channel: CliReleaseChannel,
98): string | undefined {
99 for (const release of releases) {
100 if (release.draft) continue;
101 const version = /^v(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/.exec(release.tag_name)?.[1];
102 if (version === undefined) continue;
103 if (cliReleaseChannelOf(version) === channel) return version;
104 }
105 return undefined;