Files
opencode-manager/ocm-cli/src/state.ts
T
Chris Scott 61efcf9ad1 feat(ocm-cli): add session move with local history export and TUI plugin (#304)
* loop: ocm-session-move completed after 7 iterations

* feat(ocm-cli): add session move with local history export and TUI plugin

* docs(ocm-cli): update plugin config to tui-only entry and clarify session history source

* fix(ocm-cli): replace raw postinstall output with TUI toast notification
2026-07-08 17:23:53 -04:00

73 lines
1.8 KiB
TypeScript

import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'
import { homedir } from 'os'
import { dirname, join } from 'path'
export interface OcmState {
managerUrl: string
lastRepoId?: number
lastRepoName?: string
lastRepoDir?: string
lastRepoBranch?: string | null
updatedAt?: number
}
export interface InstallNotice {
link: string
binDir: string
pathMissing: boolean
}
const STATE_DIR = join(homedir(), '.config', 'opencode-manager')
const STATE_FILE = join(STATE_DIR, 'state.json')
const INSTALL_NOTICE_FILE = join(STATE_DIR, 'install-notice.json')
export function getStatePath(): string {
return STATE_FILE
}
export function readState(): OcmState | null {
if (!existsSync(STATE_FILE)) return null
try {
const raw = readFileSync(STATE_FILE, 'utf-8')
const parsed = JSON.parse(raw) as OcmState
if (!parsed.managerUrl) return null
return parsed
} catch {
return null
}
}
export function writeState(state: OcmState): void {
mkdirSync(dirname(STATE_FILE), { recursive: true })
const next: OcmState = { ...state, updatedAt: Date.now() }
writeFileSync(STATE_FILE, JSON.stringify(next, null, 2), { mode: 0o600 })
}
export function clearState(): void {
if (existsSync(STATE_FILE)) {
writeFileSync(STATE_FILE, '{}', { mode: 0o600 })
}
}
export function readInstallNotice(): InstallNotice | null {
if (!existsSync(INSTALL_NOTICE_FILE)) return null
try {
const raw = readFileSync(INSTALL_NOTICE_FILE, 'utf-8')
const parsed = JSON.parse(raw) as InstallNotice
if (!parsed.link || !parsed.binDir) return null
return parsed
} catch {
return null
} finally {
deleteInstallNotice()
}
}
function deleteInstallNotice(): void {
try {
unlinkSync(INSTALL_NOTICE_FILE)
} catch {
return
}
}