/ utils / github / ghAuthStatus.ts
ghAuthStatus.ts
 1  import { execa } from 'execa'
 2  import { which } from '../which.js'
 3  
 4  export type GhAuthStatus =
 5    | 'authenticated'
 6    | 'not_authenticated'
 7    | 'not_installed'
 8  
 9  /**
10   * Returns gh CLI install + auth status for telemetry.
11   * Uses which() first (Bun.which — no subprocess) to detect install, then
12   * exit code of `gh auth token` to detect auth. Uses `auth token` instead of
13   * `auth status` because the latter makes a network request to GitHub's API,
14   * while `auth token` only reads local config/keyring. Spawns with
15   * stdout: 'ignore' so the token never enters this process.
16   */
17  export async function getGhAuthStatus(): Promise<GhAuthStatus> {
18    const ghPath = await which('gh')
19    if (!ghPath) {
20      return 'not_installed'
21    }
22    const { exitCode } = await execa('gh', ['auth', 'token'], {
23      stdout: 'ignore',
24      stderr: 'ignore',
25      timeout: 5000,
26      reject: false,
27    })
28    return exitCode === 0 ? 'authenticated' : 'not_authenticated'
29  }