Find the t3 CLI where the Windows installer puts it

t3tui looked only for ~/.t3/bin/t3, which Windows doesn't have, so it
couldn't get a credential from a local server there. A .cmd also has to
go through cmd.
This commit is contained in:
Callum Alpass committed 2026-11-04 16:45:03 +11:00
1 parent 159c5e8fbc
commit 53ceb6a8c5
2 files changed
+56 -2

No files matched your search

+10 -2
View File
@@ -83,15 +83,23 @@ async function writeCredential(credential: StoredCredential): Promise<void> {
await chmod(path, 0o600); await chmod(path, 0o600);
} }
/** The `t3` CLI: the desktop app's copy, or the one on the PATH, or where the Windows installer puts it. A .cmd only runs through cmd. */
export function t3Command(exists: (path: string) => boolean = existsSync, which: (program: string) => string | null = Bun.which): string[] {
const t3 =
process.env.T3TUI_T3_BIN ??
[join(t3Home(), "bin", "t3"), which("t3"), join(homedir(), ".local", "bin", "t3.cmd")].find((path): path is string => path !== null && exists(path)) ??
"t3";
return /\.(?:cmd|bat)$/i.test(t3) ? ["cmd.exe", "/c", "call", t3] : [t3];
}
/** /**
* Issues a scoped bearer session through the local `t3` CLI, which can mint one * Issues a scoped bearer session through the local `t3` CLI, which can mint one
* because it shares the server's data directory. Remote servers need T3TUI_TOKEN. * because it shares the server's data directory. Remote servers need T3TUI_TOKEN.
*/ */
export async function issueLocalCredential(target: ServerTarget): Promise<StoredCredential> { export async function issueLocalCredential(target: ServerTarget): Promise<StoredCredential> {
const t3 = process.env.T3TUI_T3_BIN ?? join(t3Home(), "bin", "t3");
const args = ["auth", "session", "issue", "--label", "t3tui", "--token-only"]; const args = ["auth", "session", "issue", "--label", "t3tui", "--token-only"];
for (const scope of T3TUI_SCOPES) args.push("--scope", scope); for (const scope of T3TUI_SCOPES) args.push("--scope", scope);
const proc = Bun.spawn([t3, ...args], { stdout: "pipe", stderr: "pipe" }); const proc = Bun.spawn([...t3Command(), ...args], { stdout: "pipe", stderr: "pipe" });
const [stdout, stderr, code] = await Promise.all([ const [stdout, stderr, code] = await Promise.all([
new Response(proc.stdout).text(), new Response(proc.stdout).text(),
new Response(proc.stderr).text(), new Response(proc.stderr).text(),
+46
View File
@@ -0,0 +1,46 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { homedir } from "node:os";
import { join } from "node:path";
import { t3Command } from "../src/client/auth.ts";
const appCopy = join(homedir(), ".t3", "bin", "t3");
const installerCopy = join(homedir(), ".local", "bin", "t3.cmd");
describe("t3Command", () => {
const saved = { bin: process.env.T3TUI_T3_BIN, home: process.env.T3CODE_HOME };
beforeEach(() => {
delete process.env.T3TUI_T3_BIN;
delete process.env.T3CODE_HOME;
});
afterEach(() => {
delete process.env.T3TUI_T3_BIN;
if (saved.bin !== undefined) process.env.T3TUI_T3_BIN = saved.bin;
if (saved.home !== undefined) process.env.T3CODE_HOME = saved.home;
});
test("prefers the desktop app's copy, then the PATH", () => {
expect(t3Command((path) => path === appCopy, () => "/usr/bin/t3")).toEqual([appCopy]);
expect(t3Command((path) => path === "/usr/bin/t3", () => "/usr/bin/t3")).toEqual(["/usr/bin/t3"]);
});
test("runs where the Windows installer puts it, which needs cmd", () => {
expect(t3Command((path) => path === installerCopy, () => null)).toEqual(["cmd.exe", "/c", "call", installerCopy]);
});
test("runs a .cmd on the PATH through cmd too", () => {
const shim = "C:\\npm\\t3.CMD";
expect(t3Command((path) => path === shim, () => shim)).toEqual(["cmd.exe", "/c", "call", shim]);
});
test("falls back to the bare name so the error names it", () => {
expect(t3Command(() => false, () => null)).toEqual(["t3"]);
});
test("T3TUI_T3_BIN wins", () => {
process.env.T3TUI_T3_BIN = "/opt/t3";
expect(t3Command(() => true, () => "/usr/bin/t3")).toEqual(["/opt/t3"]);
});
});