sandlot/src/commands/cleanup.ts

30 lines
766 B
TypeScript

import { join } from "path"
import { existsSync } from "fs"
import { unlink } from "fs/promises"
import * as git from "../git.ts"
import * as state from "../state.ts"
export async function action() {
const root = await git.repoRoot()
const st = await state.load(root)
const sessions = Object.values(st.sessions)
if (sessions.length === 0) {
console.log("No sessions to clean up.")
return
}
const stale = sessions.filter(s => !existsSync(s.worktree))
if (stale.length === 0) {
console.log("No stale sessions found.")
return
}
for (const s of stale) {
await state.removeSession(root, s.branch)
await unlink(join(root, '.sandlot', s.branch)).catch(() => {})
console.log(`✔ Removed stale session: ${s.branch}`)
}
}