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

cliRelease.ts

105 lines3.7 KB

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