sandlot/src/state.ts
2026-02-16 20:22:44 -08:00

51 lines
1.4 KiB
TypeScript

import { join } from "path"
export interface Session {
branch: string
worktree: string
vm_id: string
created_at: string
status: "running" | "stopped"
}
export interface State {
sessions: Record<string, Session>
}
function statePath(repoRoot: string): string {
return join(repoRoot, ".sandlot", "state.json")
}
export async function load(repoRoot: string): Promise<State> {
const path = statePath(repoRoot)
const file = Bun.file(path)
if (await file.exists()) {
return await file.json()
}
return { sessions: {} }
}
export async function save(repoRoot: string, state: State): Promise<void> {
const path = statePath(repoRoot)
const dir = join(repoRoot, ".sandlot")
await Bun.write(join(dir, ".gitkeep"), "") // ensure dir exists
await Bun.write(path, JSON.stringify(state, null, 2) + "\n")
}
export async function getSession(repoRoot: string, branch: string): Promise<Session | undefined> {
const state = await load(repoRoot)
return state.sessions[branch]
}
export async function setSession(repoRoot: string, session: Session): Promise<void> {
const state = await load(repoRoot)
state.sessions[session.branch] = session
await save(repoRoot, state)
}
export async function removeSession(repoRoot: string, branch: string): Promise<void> {
const state = await load(repoRoot)
delete state.sessions[branch]
await save(repoRoot, state)
}