|
| 1 | +import { homedir } from "node:os"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { type CommandExecutor, shellExecutor } from "./executor"; |
| 5 | +import { createError, err, ok, type Result } from "./result"; |
| 6 | + |
| 7 | +const AuthStateSchema = z.object({ |
| 8 | + version: z.literal(1), |
| 9 | + ghAuthenticated: z.boolean(), |
| 10 | + username: z.string().optional(), |
| 11 | +}); |
| 12 | + |
| 13 | +type AuthState = z.infer<typeof AuthStateSchema>; |
| 14 | + |
| 15 | +const AUTH_CONFIG_DIR = ".config/array"; |
| 16 | +const AUTH_FILE = "auth.json"; |
| 17 | + |
| 18 | +function getAuthPath(): string { |
| 19 | + return join(homedir(), AUTH_CONFIG_DIR, AUTH_FILE); |
| 20 | +} |
| 21 | + |
| 22 | +export async function saveAuthState(state: AuthState): Promise<void> { |
| 23 | + const authDir = join(homedir(), AUTH_CONFIG_DIR); |
| 24 | + const authPath = getAuthPath(); |
| 25 | + |
| 26 | + await ensureDir(authDir); |
| 27 | + await Bun.write(authPath, JSON.stringify(state, null, 2)); |
| 28 | +} |
| 29 | + |
| 30 | +interface GhAuthStatus { |
| 31 | + authenticated: boolean; |
| 32 | + username?: string; |
| 33 | + error?: string; |
| 34 | +} |
| 35 | + |
| 36 | +export async function checkGhAuth( |
| 37 | + executor: CommandExecutor = shellExecutor, |
| 38 | +): Promise<GhAuthStatus> { |
| 39 | + try { |
| 40 | + const result = await executor.execute("gh", ["auth", "status"], { |
| 41 | + cwd: process.cwd(), |
| 42 | + }); |
| 43 | + |
| 44 | + if (result.exitCode === 0) { |
| 45 | + const usernameMatch = result.stdout.match( |
| 46 | + /Logged in to github\.com account (\S+)/, |
| 47 | + ); |
| 48 | + const username = usernameMatch ? usernameMatch[1] : undefined; |
| 49 | + return { authenticated: true, username }; |
| 50 | + } |
| 51 | + |
| 52 | + return { authenticated: false, error: result.stderr }; |
| 53 | + } catch (e) { |
| 54 | + return { authenticated: false, error: `Failed to check gh auth: ${e}` }; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +export async function ghAuthLogin( |
| 59 | + executor: CommandExecutor = shellExecutor, |
| 60 | +): Promise<Result<string>> { |
| 61 | + try { |
| 62 | + const result = await executor.execute("gh", ["auth", "login", "--web"], { |
| 63 | + cwd: process.cwd(), |
| 64 | + }); |
| 65 | + |
| 66 | + if (result.exitCode !== 0) { |
| 67 | + return err( |
| 68 | + createError( |
| 69 | + "COMMAND_FAILED", |
| 70 | + result.stderr || "Failed to authenticate with GitHub", |
| 71 | + ), |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + const status = await checkGhAuth(executor); |
| 76 | + if (!status.authenticated) { |
| 77 | + return err(createError("COMMAND_FAILED", "Authentication failed")); |
| 78 | + } |
| 79 | + |
| 80 | + return ok(status.username || "unknown"); |
| 81 | + } catch (e) { |
| 82 | + return err(createError("COMMAND_FAILED", `Failed to authenticate: ${e}`)); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export async function isGhInstalled( |
| 87 | + executor: CommandExecutor = shellExecutor, |
| 88 | +): Promise<boolean> { |
| 89 | + try { |
| 90 | + const result = await executor.execute("which", ["gh"], { |
| 91 | + cwd: process.cwd(), |
| 92 | + }); |
| 93 | + return result.exitCode === 0; |
| 94 | + } catch { |
| 95 | + return false; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +async function ensureDir(dirPath: string): Promise<void> { |
| 100 | + try { |
| 101 | + const { mkdir } = await import("node:fs/promises"); |
| 102 | + await mkdir(dirPath, { recursive: true }); |
| 103 | + } catch { |
| 104 | + // Directory might already exist |
| 105 | + } |
| 106 | +} |
0 commit comments