()
| 19 | * Must be called before any API operations |
| 20 | */ |
| 21 | export async function initializeToken(): Promise<void> { |
| 22 | // Initialize API URL first (env > settings.json > default) |
| 23 | await initializeApiUrl() |
| 24 | |
| 25 | // 1. Environment variable has highest priority (allows temporary override) |
| 26 | if (configuration.cliApiToken) { |
| 27 | return |
| 28 | } |
| 29 | |
| 30 | // 2. Read from settings file |
| 31 | const settings = await readSettings() |
| 32 | if (settings.cliApiToken) { |
| 33 | configuration._setCliApiToken(settings.cliApiToken) |
| 34 | return |
| 35 | } |
| 36 | |
| 37 | // 3. Non-TTY environment cannot prompt, fail with clear error |
| 38 | if (!process.stdin.isTTY) { |
| 39 | throw new Error('CLI_API_TOKEN is required. Set it via environment variable or run `hapi auth login`.') |
| 40 | } |
| 41 | |
| 42 | // 4. Interactive prompt |
| 43 | const token = await promptForToken() |
| 44 | |
| 45 | // 5. Save and update configuration |
| 46 | await updateSettings(current => ({ |
| 47 | ...current, |
| 48 | cliApiToken: token |
| 49 | })) |
| 50 | configuration._setCliApiToken(token) |
| 51 | } |
| 52 | |
| 53 | async function promptForToken(): Promise<string> { |
| 54 | const rl = readline.createInterface({ input, output }) |
no test coverage detected