Files
hapi/cli/src/configuration.ts
T
weishu 9dc69d1e80 feat: add serverUrl configuration support with settings.json fallback
Implement serverUrl initialization following the cliApiToken design pattern:
- Environment variable HAPI_SERVER_URL (highest priority, allows temporary override)
- settings.json serverUrl field (user persistent configuration)
- Default value 'http://localhost:3006' (fallback)

Changes:
- Add serverUrl field to Settings interface (persistence.ts)
- Convert serverUrl to private property with getter/setter (configuration.ts)
- Create initializeServerUrl module to load from settings (serverUrlInit.ts)
- Integrate serverUrl initialization in tokenInit flow (tokenInit.ts)
- Skip auto-start when serverUrl is configured in settings (autoStartServer.ts)
2026-01-08 10:37:37 +08:00

85 lines
2.8 KiB
TypeScript

/**
* Global configuration for HAPI CLI
*
* Centralizes all configuration including environment variables and paths
* Environment files should be loaded using Node's --env-file flag
*/
import { existsSync, mkdirSync } from 'node:fs'
import { homedir } from 'node:os'
import { join } from 'node:path'
import packageJson from '../package.json'
import { getCliArgs } from '@/utils/cliArgs'
class Configuration {
private _serverUrl: string
private _cliApiToken: string
public readonly isDaemonProcess: boolean
// Directories and paths (from persistence)
public readonly happyHomeDir: string
public readonly logsDir: string
public readonly settingsFile: string
public readonly privateKeyFile: string
public readonly daemonStateFile: string
public readonly daemonLockFile: string
public readonly currentCliVersion: string
public readonly isExperimentalEnabled: boolean
constructor() {
// Server configuration
this._serverUrl = process.env.HAPI_SERVER_URL || 'http://localhost:3006'
this._cliApiToken = process.env.CLI_API_TOKEN || ''
// Check if we're running as daemon based on process args
const args = getCliArgs()
this.isDaemonProcess = args.length >= 2 && args[0] === 'daemon' && (args[1] === 'start-sync')
// Directory configuration - Priority: HAPI_HOME env > default home dir
if (process.env.HAPI_HOME) {
// Expand ~ to home directory if present
const expandedPath = process.env.HAPI_HOME.replace(/^~/, homedir())
this.happyHomeDir = expandedPath
} else {
this.happyHomeDir = join(homedir(), '.hapi')
}
this.logsDir = join(this.happyHomeDir, 'logs')
this.settingsFile = join(this.happyHomeDir, 'settings.json')
this.privateKeyFile = join(this.happyHomeDir, 'access.key')
this.daemonStateFile = join(this.happyHomeDir, 'daemon.state.json')
this.daemonLockFile = join(this.happyHomeDir, 'daemon.state.json.lock')
this.isExperimentalEnabled = ['true', '1', 'yes'].includes(process.env.HAPI_EXPERIMENTAL?.toLowerCase() || '')
this.currentCliVersion = packageJson.version
if (!existsSync(this.happyHomeDir)) {
mkdirSync(this.happyHomeDir, { recursive: true })
}
// Ensure directories exist
if (!existsSync(this.logsDir)) {
mkdirSync(this.logsDir, { recursive: true })
}
}
get serverUrl(): string {
return this._serverUrl
}
_setServerUrl(url: string): void {
this._serverUrl = url
}
get cliApiToken(): string {
return this._cliApiToken
}
_setCliApiToken(token: string): void {
this._cliApiToken = token
}
}
export const configuration: Configuration = new Configuration()