mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-06 06:41:56 +00:00
Consolidate CLI and server runtime directories from ~/.config/hapi/ (CLI) and ~/.hapi-server/ (Server) to a single ~/.hapi/ directory. Unify environment variables from HAPI_HOME_DIR (CLI) and HAPI_BOT_DATA_DIR (Server) to a single HAPI_HOME variable across both applications. Update all documentation and configuration references accordingly. Bump bun-types to 1.3.5.
66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
/**
|
|
* Token initialization module
|
|
*
|
|
* Handles CLI_API_TOKEN initialization with priority:
|
|
* 1. Environment variable (highest - allows temporary override)
|
|
* 2. Settings file (~/.hapi/settings.json)
|
|
* 3. Interactive prompt (only when both above are missing)
|
|
*/
|
|
|
|
import * as readline from 'node:readline/promises'
|
|
import { stdin as input, stdout as output } from 'node:process'
|
|
import chalk from 'chalk'
|
|
import { configuration } from '@/configuration'
|
|
import { readSettings, updateSettings } from '@/persistence'
|
|
|
|
/**
|
|
* Initialize CLI API token
|
|
* Must be called before any API operations
|
|
*/
|
|
export async function initializeToken(): Promise<void> {
|
|
// 1. Environment variable has highest priority (allows temporary override)
|
|
if (configuration.cliApiToken) {
|
|
return
|
|
}
|
|
|
|
// 2. Read from settings file
|
|
const settings = await readSettings()
|
|
if (settings.cliApiToken) {
|
|
configuration._setCliApiToken(settings.cliApiToken)
|
|
return
|
|
}
|
|
|
|
// 3. Non-TTY environment cannot prompt, fail with clear error
|
|
if (!process.stdin.isTTY) {
|
|
throw new Error('CLI_API_TOKEN is required. Set it via environment variable or run `hapi auth login`.')
|
|
}
|
|
|
|
// 4. Interactive prompt
|
|
const token = await promptForToken()
|
|
|
|
// 5. Save and update configuration
|
|
await updateSettings(current => ({
|
|
...current,
|
|
cliApiToken: token
|
|
}))
|
|
configuration._setCliApiToken(token)
|
|
}
|
|
|
|
async function promptForToken(): Promise<string> {
|
|
const rl = readline.createInterface({ input, output })
|
|
|
|
console.log(chalk.yellow('\nNo CLI_API_TOKEN found.'))
|
|
console.log(chalk.gray('You can set it via environment variable or enter it now.\n'))
|
|
|
|
try {
|
|
const token = await rl.question(chalk.cyan('Enter CLI_API_TOKEN: '))
|
|
if (!token.trim()) {
|
|
throw new Error('Token cannot be empty')
|
|
}
|
|
console.log(chalk.green(`\nToken saved to ${configuration.settingsFile}`))
|
|
return token.trim()
|
|
} finally {
|
|
rl.close()
|
|
}
|
|
}
|