Release standalone binaries with a one-line installer
Installing meant Bun, a clone with the T3 Code submodule and a build. Now a tag builds a binary per platform with bun build --compile, carrying Bun's runtime and OpenTUI's native library, and publishes them as a GitHub Release after opening each one's dashboard on its own OS. install.sh picks the right binary and checks it against SHA256SUMS; `t3tui upgrade` replaces an installed binary with the latest release and `t3tui version` prints the version. `t3tui tmux` started its windows as `bun <file>`, which a compiled binary can't do, so it now runs itself.
This commit is contained in:
1 parent
c0b7615590
commit
9bf9abb540
10 files changed
+319
-2
No files matched your search
+12
-1
@@ -3,6 +3,7 @@ import type { OrchestrationV2ShellSnapshot, ThreadId } from "@t3tools/contracts"
|
||||
|
||||
import { migrateConfigDir } from "./client/auth.ts";
|
||||
import { T3Session } from "./client/session.ts";
|
||||
import { isCompiled, upgrade, VERSION } from "./upgrade.ts";
|
||||
import { findThread, lastActivity, relativeTime, rowTime, shortId, statusLabel, threadRows } from "./model/threads.ts";
|
||||
|
||||
const USAGE = `t3tui: a terminal client for T3 Code
|
||||
@@ -13,6 +14,8 @@ usage:
|
||||
t3tui list [--json] print threads and exit
|
||||
t3tui tmux [--watch] [--session <name>]
|
||||
mirror active threads into tmux windows (default session: t3tui)
|
||||
t3tui upgrade replace this binary with the latest release
|
||||
t3tui version
|
||||
t3tui help
|
||||
|
||||
env:
|
||||
@@ -35,6 +38,13 @@ async function main(argv: string[]) {
|
||||
return list(rest.includes("--json"));
|
||||
case "tmux":
|
||||
return tmuxCommand(rest);
|
||||
case "upgrade":
|
||||
return upgrade();
|
||||
case "version":
|
||||
case "--version":
|
||||
case "-v":
|
||||
console.log(`t3tui ${VERSION}`);
|
||||
return;
|
||||
case "help":
|
||||
case "--help":
|
||||
case "-h":
|
||||
@@ -83,7 +93,8 @@ async function tmuxCommand(args: string[]) {
|
||||
const sessionFlag = args.indexOf("--session");
|
||||
const name = sessionFlag >= 0 ? args[sessionFlag + 1] : "t3tui";
|
||||
if (!name) return fail("usage: t3tui tmux [--watch] [--session <name>]");
|
||||
const options = { session: name, t3tui: [process.execPath, import.meta.path] };
|
||||
// A compiled binary is t3tui by itself; otherwise it's Bun running this file.
|
||||
const options = { session: name, t3tui: isCompiled() ? [process.execPath] : [process.execPath, import.meta.path] };
|
||||
const { session, snapshot } = await connectedSession();
|
||||
const { opened } = await syncTmux(snapshot, options);
|
||||
console.log(`tmux session "${name}" is in sync (${opened} new window${opened === 1 ? "" : "s"}). Attach with: tmux attach -t ${name}`);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
// `t3tui upgrade`: replaces a released binary with the latest release's.
|
||||
import { chmod, rename, rm } from "node:fs/promises";
|
||||
import { realpathSync } from "node:fs";
|
||||
|
||||
import { version } from "../package.json" with { type: "json" };
|
||||
|
||||
export const VERSION = version;
|
||||
const REPO = "callumalpass/t3tui";
|
||||
|
||||
/** A compiled binary runs its code from Bun's embedded filesystem. */
|
||||
export function isCompiled(): boolean {
|
||||
return import.meta.path.startsWith("/$bunfs/");
|
||||
}
|
||||
|
||||
/** The release asset for this machine, e.g. t3tui-linux-x64. */
|
||||
export function assetName(platform: string = process.platform, arch: string = process.arch): string {
|
||||
return `t3tui-${platform}-${arch}`;
|
||||
}
|
||||
|
||||
/** Finds an asset's hash in a sha256sum listing. */
|
||||
export function checksumFor(sums: string, file: string): string | undefined {
|
||||
for (const line of sums.split("\n")) {
|
||||
const [hash, name] = line.trim().split(/\s+\*?/);
|
||||
if (name === file) return hash;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export async function upgrade(): Promise<void> {
|
||||
if (!isCompiled()) {
|
||||
console.log("t3tui is running from source. Update it with: git pull --recurse-submodules && bun install");
|
||||
return;
|
||||
}
|
||||
const latest = await fetch(`https://api.github.com/repos/${REPO}/releases/latest`, { headers: { accept: "application/vnd.github+json" } });
|
||||
if (latest.status === 404) throw new Error("No t3tui release has been published yet.");
|
||||
if (!latest.ok) throw new Error(`Couldn't check for a new release (GitHub answered ${latest.status}).`);
|
||||
const tag = ((await latest.json()) as { tag_name: string }).tag_name;
|
||||
if (tag === `v${VERSION}`) {
|
||||
console.log(`t3tui ${VERSION} is the latest release.`);
|
||||
return;
|
||||
}
|
||||
const file = `${assetName()}.gz`;
|
||||
const base = `https://github.com/${REPO}/releases/download/${tag}`;
|
||||
const [archive, sums] = await Promise.all([download(`${base}/${file}`), download(`${base}/SHA256SUMS`)]);
|
||||
const expected = checksumFor(new TextDecoder().decode(sums), file);
|
||||
if (!expected) throw new Error(`Release ${tag} has no ${file}.`);
|
||||
const actual = new Bun.CryptoHasher("sha256").update(archive).digest("hex");
|
||||
if (actual !== expected) throw new Error(`The downloaded ${file} doesn't match its checksum; nothing was changed.`);
|
||||
|
||||
// Renaming over the running binary is safe: the running process keeps the old file open.
|
||||
const target = realpathSync(process.execPath);
|
||||
const temporary = `${target}.upgrade-${process.pid}`;
|
||||
try {
|
||||
await Bun.write(temporary, Bun.gunzipSync(archive));
|
||||
await chmod(temporary, 0o755);
|
||||
await rename(temporary, target);
|
||||
} catch (error) {
|
||||
await rm(temporary, { force: true });
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
if (code === "EACCES" || code === "EPERM") throw new Error(`Can't write ${target}. Rerun with permission to replace it (e.g. sudo t3tui upgrade).`);
|
||||
throw error;
|
||||
}
|
||||
console.log(`t3tui ${VERSION} → ${tag.replace(/^v/, "")}`);
|
||||
}
|
||||
|
||||
async function download(url: string): Promise<Uint8Array<ArrayBuffer>> {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error(`Couldn't download ${url} (${response.status}).`);
|
||||
return new Uint8Array(await response.arrayBuffer());
|
||||
}
|
||||
Reference in new issue
Block a user