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

cliRelease.ts

115 lines4.5 KB

1/**
2 * Naming shared by the release workflow, the runtime installers, and
3 * install scripts for the per-platform CLI archives attached to GitHub
4 * Releases. Every consumer derives the same file names from a version and a
5 * platform key, so a rename here is a release-breaking change.
6 */
7​
8const CLI_RELEASE_REPOSITORY = "pingdotgg/t3code";
9export const CLI_RELEASE_CHECKSUMS_FILE = "SHA256SUMS";
10/** Overrides the download origin for mirrors and air-gapped installs. */
11export const CLI_RELEASE_BASE_URL_ENV = "T3CODE_RELEASE_BASE_URL";
12​
13/**
14 * The archives a release actually attaches. Kept in step with the
15 * `cli_archive` matrix flags in .github/workflows/release.yml: a key here
16 * without a build there produces download URLs that 404, and a build there
17 * without a key here is unreachable from every installer.
18 */
19const CLI_ARCHIVE_PLATFORM_KEYS = ["darwin-arm64", "linux-x64", "win32-x64"] 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 * The tar to extract a release archive with. Windows ships bsdtar in
32 * System32, which reads both formats; a Git-for-Windows GNU tar earlier on
33 * PATH cannot open the zip, so the system copy is named by 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​
50/** Directory that `releases/download/<tag>/<asset>` lives under. */
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​
58/**
59 * Parses the `sha256sum` style checksum file attached to each release.
60 * Lines are `<hex> <file>`; a leading `*` marks binary mode and is ignored.
61 */
62export function parseChecksums(text: string): ReadonlyMap<string, string> {
63 const checksums = new Map<string, string>();
64 for (const line of text.split(/\r?\n/)) {
65 const match = /^([0-9a-fA-F]{64})\s+\*?(\S.*)$/.exec(line.trim());
66 if (match?.[1] !== undefined && match[2] !== undefined) {
67 checksums.set(match[2], match[1].toLowerCase());
68 }
69 }
70 return checksums;
72​
73export type CliReleaseChannel = "stable" | "nightly" | "preview";
74export const CLI_RELEASE_CHANNELS: ReadonlyArray<CliReleaseChannel> = [
75 "stable",
76 "nightly",
77 "preview",
78];
79​
80/** The release train a version was published on, derived from its prerelease tag. */
81export function cliReleaseChannelOf(version: string): CliReleaseChannel {
82 const channel = /^[^-+]+-(nightly|preview)\.\d{8}\.\d+$/.exec(version)?.[1];
83 return channel === "nightly" || channel === "preview" ? channel : "stable";
85​
86/**
87 * One page of GitHub's list-releases endpoint, newest first. Callers walk pages
88 * until a channel match turns up; a busy nightly train can push the newest
89 * preview or stable release past any single page.
90 */
91export function cliReleaseIndexPageUrl(page: number): string {
92 return `https://api.github.com/repos/${CLI_RELEASE_REPOSITORY}/releases?per_page=100&page=${page}`;
94​
95/**
96 * Picks the newest version on a channel from the release index. Tags are
97 * `v<version>`; the channel is decided by the same rule the runtime uses, so
98 * a preview tag never satisfies a nightly lookup and vice versa. Drafts are
99 * skipped because their assets are not downloadable.
100 */
101export function newestCliReleaseVersion(
102 releases: ReadonlyArray<{
103 readonly tag_name: string;
104 readonly draft?: boolean | undefined;
105 }>,
106 channel: CliReleaseChannel,
107): string | undefined {
108 for (const release of releases) {
109 if (release.draft) continue;
110 const version = /^v(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/.exec(release.tag_name)?.[1];
111 if (version === undefined) continue;
112 if (cliReleaseChannelOf(version) === channel) return version;
113 }
114 return undefined;