Open History to see the last commit
1export interface TanStackSourceAuthAccount {
2 readonly host: string;
3 readonly account: string;
4 readonly authenticated: boolean;
5 readonly active: boolean;
6 readonly error: string | null;
7 /** Set when the login is `SRC_TOKEN`, which `src` puts before every saved login. */
8 readonly environmentVariable: string | null;
9}
10
11const HOST_LINE = /^([a-z0-9][a-z0-9.-]*(?::\d+)?)$/iu;
12const ACCOUNT_LINE =
13 /^\s*(?:✓|X)\s+(Logged in|Failed to log in) to (\S+) (?:account (\S+)(?: \(([A-Z][A-Z0-9_]*)\))?|using token \(([A-Z][A-Z0-9_]*)\))/u;
14const ACTIVE_LINE = /^\s*-\s+Active account:\s+(true|false)\s*$/u;
15const DETAIL_LINE = /^\s*-\s+(.+)$/u;
16const SAFE_DETAIL = /^(?!Token:|Git operations protocol:)/u;
17
18/**
19 * `src auth status` speaks `gh auth status`'s text (it has no `--json`): a host line, then per
20 * login a `✓`/`X` line and indented `- key: value` lines, the last of a failed one its reason.
21 */
22export function parseTanStackSourceAuthStatus(
23 text: string,
24): ReadonlyArray<TanStackSourceAuthAccount> {
25 const accounts: Array<TanStackSourceAuthAccount> = [];
26 let current: {
27 host: string;
28 account: string;
29 authenticated: boolean;
30 active: boolean;
31 error: string | null;
32 environmentVariable: string | null;
33 } | null = null;
34 for (const line of text.split(/\r?\n/u)) {
35 if (HOST_LINE.test(line.trim()) && !line.startsWith(" ")) {
36 current = null;
37 continue;
38 }
39 const login = ACCOUNT_LINE.exec(line);
40 if (login !== null) {
41 const variable = login[4] ?? login[5] ?? null;
42 current = {
43 host: login[2]!.toLowerCase(),
44 // A refused SRC_TOKEN names no account; it is reported, but is no login to pick.
45 account: login[3] ?? "",
46 authenticated: login[1] === "Logged in",
47 active: false,
48 error: null,
49 environmentVariable: variable,
50 };
51 accounts.push(current);
52 continue;
53 }
54 if (current === null) continue;
55 const active = ACTIVE_LINE.exec(line);
56 if (active !== null) {
57 current.active = active[1] === "true";
58 continue;
59 }
60 const detail = DETAIL_LINE.exec(line)?.[1]?.trim();
61 if (!current.authenticated && detail !== undefined && SAFE_DETAIL.test(detail))
62 current.error = detail;
63 }
64 return accounts;
65}