2026-05-16 20:27:23 +08:00
|
|
|
import { join } from 'path'
|
2026-05-19 16:09:59 +08:00
|
|
|
import { readFileSync, existsSync, readdirSync } from 'fs'
|
2026-05-16 20:27:23 +08:00
|
|
|
import { detectHermesRootHome } from './hermes-path'
|
2026-04-16 13:51:42 +08:00
|
|
|
|
2026-05-16 20:27:23 +08:00
|
|
|
export function getHermesBaseDir(): string {
|
|
|
|
|
return detectHermesRootHome()
|
|
|
|
|
}
|
2026-04-16 13:51:42 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the active profile's home directory.
|
|
|
|
|
* default → ~/.hermes/
|
|
|
|
|
* other → ~/.hermes/profiles/{name}/
|
|
|
|
|
*/
|
|
|
|
|
export function getActiveProfileDir(): string {
|
2026-05-16 20:27:23 +08:00
|
|
|
const hermesBase = getHermesBaseDir()
|
|
|
|
|
const activeFile = join(hermesBase, 'active_profile')
|
2026-04-16 13:51:42 +08:00
|
|
|
try {
|
|
|
|
|
const name = readFileSync(activeFile, 'utf-8').trim()
|
|
|
|
|
if (name && name !== 'default') {
|
2026-05-16 20:27:23 +08:00
|
|
|
const dir = join(hermesBase, 'profiles', name)
|
2026-04-16 13:51:42 +08:00
|
|
|
if (existsSync(dir)) return dir
|
|
|
|
|
}
|
|
|
|
|
} catch { }
|
2026-05-16 20:27:23 +08:00
|
|
|
return hermesBase
|
2026-04-16 13:51:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the active profile's config.yaml path.
|
|
|
|
|
*/
|
|
|
|
|
export function getActiveConfigPath(): string {
|
|
|
|
|
return join(getActiveProfileDir(), 'config.yaml')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the active profile's auth.json path.
|
|
|
|
|
*/
|
|
|
|
|
export function getActiveAuthPath(): string {
|
|
|
|
|
return join(getActiveProfileDir(), 'auth.json')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the active profile's .env path.
|
|
|
|
|
*/
|
|
|
|
|
export function getActiveEnvPath(): string {
|
|
|
|
|
return join(getActiveProfileDir(), '.env')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the active profile name.
|
|
|
|
|
*/
|
|
|
|
|
export function getActiveProfileName(): string {
|
2026-05-16 20:27:23 +08:00
|
|
|
const activeFile = join(getHermesBaseDir(), 'active_profile')
|
2026-04-16 13:51:42 +08:00
|
|
|
try {
|
|
|
|
|
const name = readFileSync(activeFile, 'utf-8').trim()
|
|
|
|
|
return name || 'default'
|
|
|
|
|
} catch {
|
|
|
|
|
return 'default'
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-19 20:59:25 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get profile directory by name.
|
|
|
|
|
* default → ~/.hermes/
|
|
|
|
|
* other → ~/.hermes/profiles/{name}/
|
|
|
|
|
*/
|
|
|
|
|
export function getProfileDir(name: string): string {
|
2026-05-16 20:27:23 +08:00
|
|
|
const hermesBase = getHermesBaseDir()
|
|
|
|
|
if (!name || name === 'default') return hermesBase
|
|
|
|
|
const dir = join(hermesBase, 'profiles', name)
|
|
|
|
|
return existsSync(dir) ? dir : hermesBase
|
2026-04-19 20:59:25 +08:00
|
|
|
}
|
2026-05-19 16:09:59 +08:00
|
|
|
|
|
|
|
|
export function listProfileNamesFromDisk(): string[] {
|
|
|
|
|
const hermesBase = getHermesBaseDir()
|
|
|
|
|
const names = new Set<string>(['default'])
|
|
|
|
|
const profilesDir = join(hermesBase, 'profiles')
|
|
|
|
|
try {
|
|
|
|
|
for (const entry of readdirSync(profilesDir, { withFileTypes: true })) {
|
|
|
|
|
if (entry.isDirectory() && entry.name.trim()) {
|
|
|
|
|
names.add(entry.name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {}
|
|
|
|
|
return [...names].sort((a, b) => {
|
|
|
|
|
if (a === 'default') return -1
|
|
|
|
|
if (b === 'default') return 1
|
|
|
|
|
return a.localeCompare(b)
|
|
|
|
|
})
|
|
|
|
|
}
|