Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DgBEW6qAwgn2fcQbA2f4ZQ
21 lines
561 B
TypeScript
21 lines
561 B
TypeScript
import { execFileSync } from 'node:child_process';
|
|
|
|
let cached: string | null = null;
|
|
|
|
export function getToken(): string {
|
|
if (cached) return cached;
|
|
const env = process.env.GITHUB_TOKEN;
|
|
if (env) {
|
|
cached = env;
|
|
return cached;
|
|
}
|
|
cached = execFileSync('gh', ['auth', 'token'], { encoding: 'utf8' }).trim();
|
|
if (!cached) throw new Error('gh auth token returned an empty token');
|
|
return cached;
|
|
}
|
|
|
|
/** Drop the cached token (called once on a 401 so the next call re-fetches). */
|
|
export function invalidateToken(): void {
|
|
cached = null;
|
|
}
|