UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

228 lines (226 loc) • 5.05 kB
import { existsSync, readdirSync, rmSync, statSync } from "node:fs"; import { join } from "node:path"; //#region src/services/clean.ts /** The absolute never-touch list — truth (PGlite data), identity (session * credentials), machine prefs (port pins), and the publish accelerator whose * cleaning would silently cost full re-uploads. `bos clean` reports these as * kept, always. */ const CLEAN_NEVER_TOUCH = [ { name: "db", reason: "PGlite data — the app's actual state" }, { name: "session.json", reason: "login credentials — bos logout, not bos clean" }, { name: "infra-state.json", reason: "machine port prefs" }, { name: "deploy-state.json", reason: "skip-unchanged accelerator — cleaning costs full re-uploads" } ]; /** Files touched within this window are never cleaned — a live dev session * is appending to its log and `bos logs` reads it. */ const LIVE_LOG_WINDOW_MS = 36e5; function bosPath(configDir, name) { return join(configDir, ".bos", name); } function planClean(configDir) { const removable = [ { id: "logs", path: bosPath(configDir, "logs"), kind: "files" }, { id: "bundles", path: bosPath(configDir, "bundles"), kind: "dir" }, { id: "regression", path: bosPath(configDir, "regression"), kind: "dir" }, { id: "generated", path: bosPath(configDir, "generated"), kind: "dir" }, { id: "bundle-cache", path: bosPath(configDir, "bundle-cache"), kind: "dir" }, { id: "snapshot", path: bosPath(configDir, "snapshot"), kind: "dir" }, { id: "sync-backup", path: bosPath(configDir, "sync-backup"), kind: "dir" }, { id: "import-backup", path: bosPath(configDir, "import-backup"), kind: "dir" }, { id: "compose-retired", path: bosPath(configDir, "compose-retired"), kind: "dir" }, { id: "manifest-cache", path: bosPath(configDir, "manifest.json"), kind: "file" }, { id: "resolved-config", path: bosPath(configDir, "bos.resolved-config.json"), kind: "file" }, { id: "build-cache", path: bosPath(configDir, "build-cache.json"), kind: "file" } ]; const guarded = CLEAN_NEVER_TOUCH.map(({ name, reason }) => ({ id: name, path: bosPath(configDir, name), kind: existsSync(bosPath(configDir, name)) ? "dir" : "file", guarded: reason })); return [...removable, ...guarded]; } function dirSize(path) { let total = 0; try { for (const name of readdirSync(path)) { const full = join(path, name); const stat = statSync(full); if (stat.isDirectory()) total += dirSize(full); else total += stat.size; } } catch { return total; } return total; } function removeTree(target) { const bytes = dirSize(target.path); rmSync(target.path, { recursive: true, force: true }); return bytes; } function removeFile(target) { const bytes = statSync(target.path).size; rmSync(target.path, { force: true }); return bytes; } function cleanLogs(target, dryRun) { const now = Date.now(); let bytes = 0; let fresh = 0; let names = []; try { names = readdirSync(target.path); } catch { return { id: target.id, path: target.path, bytes: 0, action: "absent" }; } for (const name of names) { const full = join(target.path, name); let size = 0; let mtimeMs = 0; try { const stat = statSync(full); if (!stat.isFile()) continue; size = stat.size; mtimeMs = stat.mtimeMs; } catch { continue; } if (now - mtimeMs < LIVE_LOG_WINDOW_MS) { fresh++; continue; } bytes += size; if (!dryRun) rmSync(full, { force: true }); } return { id: target.id, path: target.path, bytes, action: bytes > 0 || fresh > 0 ? dryRun ? "would-remove" : "removed" : "absent", ...fresh > 0 ? { reason: `${fresh} fresh file(s) kept (live sessions)` } : {} }; } function executeClean(configDir, opts = {}) { const dryRun = opts.dryRun ?? false; const skip = new Set(opts.skip ?? []); const entries = []; let freedBytes = 0; for (const target of planClean(configDir)) { if (skip.has(target.id)) continue; if (target.guarded) { entries.push({ id: target.id, path: target.path, bytes: 0, action: "kept", reason: target.guarded }); continue; } if (!existsSync(target.path)) { entries.push({ id: target.id, path: target.path, bytes: 0, action: "absent" }); continue; } if (target.kind === "files") { const entry = cleanLogs(target, dryRun); entries.push(entry); freedBytes += entry.bytes; continue; } const bytes = target.kind === "dir" ? dirSize(target.path) : statSync(target.path).size; if (!dryRun) { if (target.kind === "dir") removeTree(target); else removeFile(target); } entries.push({ id: target.id, path: target.path, bytes, action: dryRun ? "would-remove" : "removed" }); freedBytes += bytes; } return { status: "ok", dryRun, entries, freedBytes }; } //#endregion export { CLEAN_NEVER_TOUCH, dirSize, executeClean, planClean }; //# sourceMappingURL=clean.mjs.map