|
| 1 | +import { existsSync, readdirSync, rmSync, statSync } from "node:fs"; |
| 2 | +import { join } from "node:path"; |
| 3 | + |
| 4 | +/** |
| 5 | + * `bos clean` — the local-only rule's exit valve (ADR 0047): one command that |
| 6 | + * removes regenerable `.bos` state and refuses the artifacts that are truth |
| 7 | + * or identity. The partition is the contract; the handler just runs it. |
| 8 | + */ |
| 9 | + |
| 10 | +export interface CleanTarget { |
| 11 | + /** dotted category id — the report key */ |
| 12 | + id: string; |
| 13 | + /** absolute path under the config dir */ |
| 14 | + path: string; |
| 15 | + /** `dir` removes the tree, `files` removes matching files in place (logs — |
| 16 | + * the dir itself stays; a live session's fresh log survives), `file` |
| 17 | + * removes a single file */ |
| 18 | + kind: "dir" | "files" | "file"; |
| 19 | + /** present ⇒ never deleted: the never-touch invariant, with the reason */ |
| 20 | + guarded?: string; |
| 21 | +} |
| 22 | + |
| 23 | +/** The absolute never-touch list — truth (PGlite data), identity (session |
| 24 | + * credentials), machine prefs (port pins), and the publish accelerator whose |
| 25 | + * cleaning would silently cost full re-uploads. `bos clean` reports these as |
| 26 | + * kept, always. */ |
| 27 | +export const CLEAN_NEVER_TOUCH: ReadonlyArray<{ name: string; reason: string }> = [ |
| 28 | + { name: "db", reason: "PGlite data — the app's actual state" }, |
| 29 | + { name: "session.json", reason: "login credentials — bos logout, not bos clean" }, |
| 30 | + { name: "infra-state.json", reason: "machine port prefs" }, |
| 31 | + { |
| 32 | + name: "deploy-state.json", |
| 33 | + reason: "skip-unchanged accelerator — cleaning costs full re-uploads", |
| 34 | + }, |
| 35 | +]; |
| 36 | + |
| 37 | +/** Files touched within this window are never cleaned — a live dev session |
| 38 | + * is appending to its log and `bos logs` reads it. */ |
| 39 | +const LIVE_LOG_WINDOW_MS = 60 * 60 * 1000; |
| 40 | + |
| 41 | +function bosPath(configDir: string, name: string): string { |
| 42 | + return join(configDir, ".bos", name); |
| 43 | +} |
| 44 | + |
| 45 | +export function planClean(configDir: string): CleanTarget[] { |
| 46 | + const removable: CleanTarget[] = [ |
| 47 | + { id: "logs", path: bosPath(configDir, "logs"), kind: "files" }, |
| 48 | + { id: "bundles", path: bosPath(configDir, "bundles"), kind: "dir" }, |
| 49 | + { id: "regression", path: bosPath(configDir, "regression"), kind: "dir" }, |
| 50 | + { id: "generated", path: bosPath(configDir, "generated"), kind: "dir" }, |
| 51 | + { id: "bundle-cache", path: bosPath(configDir, "bundle-cache"), kind: "dir" }, |
| 52 | + { id: "snapshot", path: bosPath(configDir, "snapshot"), kind: "dir" }, |
| 53 | + { id: "sync-backup", path: bosPath(configDir, "sync-backup"), kind: "dir" }, |
| 54 | + { id: "import-backup", path: bosPath(configDir, "import-backup"), kind: "dir" }, |
| 55 | + { id: "compose-retired", path: bosPath(configDir, "compose-retired"), kind: "dir" }, |
| 56 | + { id: "manifest-cache", path: bosPath(configDir, "manifest.json"), kind: "file" }, |
| 57 | + { id: "resolved-config", path: bosPath(configDir, "bos.resolved-config.json"), kind: "file" }, |
| 58 | + { id: "build-cache", path: bosPath(configDir, "build-cache.json"), kind: "file" }, |
| 59 | + ]; |
| 60 | + const guarded: CleanTarget[] = CLEAN_NEVER_TOUCH.map(({ name, reason }) => ({ |
| 61 | + id: name, |
| 62 | + path: bosPath(configDir, name), |
| 63 | + kind: existsSync(bosPath(configDir, name)) ? "dir" : "file", |
| 64 | + guarded: reason, |
| 65 | + })); |
| 66 | + return [...removable, ...guarded]; |
| 67 | +} |
| 68 | + |
| 69 | +export function dirSize(path: string): number { |
| 70 | + let total = 0; |
| 71 | + try { |
| 72 | + for (const name of readdirSync(path)) { |
| 73 | + const full = join(path, name); |
| 74 | + const stat = statSync(full); |
| 75 | + if (stat.isDirectory()) { |
| 76 | + total += dirSize(full); |
| 77 | + } else { |
| 78 | + total += stat.size; |
| 79 | + } |
| 80 | + } |
| 81 | + } catch { |
| 82 | + return total; |
| 83 | + } |
| 84 | + return total; |
| 85 | +} |
| 86 | + |
| 87 | +export interface CleanEntryResult { |
| 88 | + id: string; |
| 89 | + path: string; |
| 90 | + bytes: number; |
| 91 | + action: "removed" | "would-remove" | "kept" | "absent"; |
| 92 | + /** `kept` — the never-touch reason; `removed` on logs — fresh files skipped */ |
| 93 | + reason?: string; |
| 94 | +} |
| 95 | + |
| 96 | +export interface CleanReport { |
| 97 | + status: "ok"; |
| 98 | + dryRun: boolean; |
| 99 | + entries: CleanEntryResult[]; |
| 100 | + freedBytes: number; |
| 101 | +} |
| 102 | + |
| 103 | +function removeTree(target: CleanTarget): number { |
| 104 | + const bytes = dirSize(target.path); |
| 105 | + rmSync(target.path, { recursive: true, force: true }); |
| 106 | + return bytes; |
| 107 | +} |
| 108 | + |
| 109 | +function removeFile(target: CleanTarget): number { |
| 110 | + const bytes = statSync(target.path).size; |
| 111 | + rmSync(target.path, { force: true }); |
| 112 | + return bytes; |
| 113 | +} |
| 114 | + |
| 115 | +function cleanLogs(target: CleanTarget, dryRun: boolean): CleanEntryResult { |
| 116 | + const now = Date.now(); |
| 117 | + let bytes = 0; |
| 118 | + let fresh = 0; |
| 119 | + let names: string[] = []; |
| 120 | + try { |
| 121 | + names = readdirSync(target.path); |
| 122 | + } catch { |
| 123 | + return { id: target.id, path: target.path, bytes: 0, action: "absent" }; |
| 124 | + } |
| 125 | + for (const name of names) { |
| 126 | + const full = join(target.path, name); |
| 127 | + let size = 0; |
| 128 | + let mtimeMs = 0; |
| 129 | + try { |
| 130 | + const stat = statSync(full); |
| 131 | + if (!stat.isFile()) continue; |
| 132 | + size = stat.size; |
| 133 | + mtimeMs = stat.mtimeMs; |
| 134 | + } catch { |
| 135 | + continue; |
| 136 | + } |
| 137 | + if (now - mtimeMs < LIVE_LOG_WINDOW_MS) { |
| 138 | + fresh++; |
| 139 | + continue; |
| 140 | + } |
| 141 | + bytes += size; |
| 142 | + if (!dryRun) rmSync(full, { force: true }); |
| 143 | + } |
| 144 | + return { |
| 145 | + id: target.id, |
| 146 | + path: target.path, |
| 147 | + bytes, |
| 148 | + action: bytes > 0 || fresh > 0 ? (dryRun ? "would-remove" : "removed") : "absent", |
| 149 | + ...(fresh > 0 ? { reason: `${fresh} fresh file(s) kept (live sessions)` } : {}), |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | +export function executeClean(configDir: string, opts: { dryRun?: boolean } = {}): CleanReport { |
| 154 | + const dryRun = opts.dryRun ?? false; |
| 155 | + const entries: CleanEntryResult[] = []; |
| 156 | + let freedBytes = 0; |
| 157 | + |
| 158 | + for (const target of planClean(configDir)) { |
| 159 | + if (target.guarded) { |
| 160 | + entries.push({ |
| 161 | + id: target.id, |
| 162 | + path: target.path, |
| 163 | + bytes: 0, |
| 164 | + action: "kept", |
| 165 | + reason: target.guarded, |
| 166 | + }); |
| 167 | + continue; |
| 168 | + } |
| 169 | + if (!existsSync(target.path)) { |
| 170 | + entries.push({ id: target.id, path: target.path, bytes: 0, action: "absent" }); |
| 171 | + continue; |
| 172 | + } |
| 173 | + if (target.kind === "files") { |
| 174 | + const entry = cleanLogs(target, dryRun); |
| 175 | + entries.push(entry); |
| 176 | + freedBytes += entry.bytes; |
| 177 | + continue; |
| 178 | + } |
| 179 | + const bytes = target.kind === "dir" ? dirSize(target.path) : statSync(target.path).size; |
| 180 | + if (!dryRun) { |
| 181 | + if (target.kind === "dir") removeTree(target); |
| 182 | + else removeFile(target); |
| 183 | + } |
| 184 | + entries.push({ |
| 185 | + id: target.id, |
| 186 | + path: target.path, |
| 187 | + bytes, |
| 188 | + action: dryRun ? "would-remove" : "removed", |
| 189 | + }); |
| 190 | + freedBytes += bytes; |
| 191 | + } |
| 192 | + |
| 193 | + return { status: "ok", dryRun, entries, freedBytes }; |
| 194 | +} |
0 commit comments