import { homedir } from 'os' import { join } from 'path' import { readFileSync } from 'fs' import { getRuntimeMetadataPath, type RuntimeMetadata } from '../../shared/runtime-bootstrap' import { RuntimeClientError } from './types' export function readMetadata(userDataPath: string): RuntimeMetadata { const metadataPath = getRuntimeMetadataPath(userDataPath) try { const metadata = JSON.parse(readFileSync(metadataPath, 'utf8')) as RuntimeMetadata | null if (!metadata?.transport || !metadata.authToken) { throw new RuntimeClientError( 'runtime_unavailable', `Orca runtime metadata is incomplete at ${metadataPath}` ) } return metadata } catch (error) { if (error instanceof RuntimeClientError) { throw error } throw new RuntimeClientError( 'runtime_unavailable', `Could not read Orca runtime metadata at ${metadataPath}. Start the Orca app first.` ) } } export function tryReadMetadata(userDataPath: string): RuntimeMetadata | null { const metadataPath = getRuntimeMetadataPath(userDataPath) try { return JSON.parse(readFileSync(metadataPath, 'utf8')) as RuntimeMetadata | null } catch { return null } } export function getDefaultUserDataPath( platform: NodeJS.Platform = process.platform, homeDir = homedir() ): string { // Why: in dev mode (and for parallel Orca instances), the Electron app writes // runtime metadata to a separate userData directory (e.g. `orca-dev`) to avoid // clobbering the production app's metadata. The CLI needs to find the same // metadata file, so this env var lets the CLI target a specific instance. if (process.env.ORCA_USER_DATA_PATH) { return process.env.ORCA_USER_DATA_PATH } if (platform === 'darwin') { return join(homeDir, 'Library', 'Application Support', 'orca') } if (platform === 'win32') { const appData = process.env.APPDATA if (!appData) { throw new RuntimeClientError( 'runtime_unavailable', 'APPDATA is not set, so the Orca runtime metadata path cannot be resolved.' ) } return join(appData, 'orca') } // Why: the CLI must find the same metadata file Electron writes in packaged // runs, so this mirrors Electron's default userData base instead of inventing // a CLI-specific config path. return join(process.env.XDG_CONFIG_HOME || join(homeDir, '.config'), 'orca') }