#!/usr/bin/env node // Removes regenerable video-production caches (Chrome profiles, raw recordings // whose final MP4 exists, per-scene segments and TTS audio caches). // Usage: node cleanup-intermediates.mjs [--apply] import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const here = path.dirname(fileURLToPath(import.meta.url)); const webRoot = path.resolve(here, "../.."); const apply = process.argv.includes("--apply"); const sizeOf = target => { let total = 0; const stack = [target]; while (stack.length) { const current = stack.pop(); let stat; try { stat = fs.lstatSync(current); } catch { continue; } if (stat.isDirectory()) { for (const entry of fs.readdirSync(current)) stack.push(path.join(current, entry)); } else if (stat.isFile()) total += stat.size; } return total; }; const seen = new Set(); const remove = (target, reason, items) => { if (!fs.existsSync(target) || seen.has(target)) return; seen.add(target); const bytes = sizeOf(target); items.push({ target: path.relative(webRoot, target), mb: Math.round(bytes / 1048576), reason }); if (apply) fs.rmSync(target, { recursive: true, force: true }); }; const items = []; // Course tools with course.json -> media mapping. for (const tool of ["line-course", "delta-course", "carrier-course"]) { const toolDir = path.join(webRoot, "tools", tool); const coursePath = path.join(toolDir, "course.json"); if (!fs.existsSync(coursePath)) continue; const course = JSON.parse(fs.readFileSync(coursePath, "utf8")); const mediaDir = path.join(webRoot, "teaching", "media", tool); for (const episode of course.episodes) { const finalMp4 = path.join(mediaDir, `${episode.id}-${episode.slug}.mp4`); const evidence = path.join(toolDir, "evidence", episode.id, "verification.json"); const hasFinal = fs.existsSync(finalMp4) && fs.existsSync(evidence); const rawDir = path.join(toolDir, "raw", episode.id); remove(path.join(rawDir, "profile"), "Chrome profile cache", items); if (hasFinal) { remove(path.join(rawDir, "screen-recording.mp4"), "raw recording (final MP4 exists)", items); remove(path.join(toolDir, "episodes", episode.id, "segments"), "scene segments (final MP4 exists)", items); remove(path.join(toolDir, "episodes", episode.id, "audio"), "TTS audio cache (final MP4 exists)", items); } } for (const name of fs.existsSync(path.join(toolDir, "raw")) ? fs.readdirSync(path.join(toolDir, "raw")) : []) { remove(path.join(toolDir, "raw", name, "profile"), "Chrome profile cache", items); } } // Standalone video projects whose final tour videos already exist. for (const project of ["carrier-video", "bim-workshop-video"]) { const projectDir = path.join(webRoot, "tools", project); if (!fs.existsSync(projectDir)) continue; // Only remove profiles, raw, segments, audio; keep scripts/cards/stills. for (const name of ["profile", "raw", "segments", "audio"]) { remove(path.join(projectDir, name), "video production cache", items); } } // Stale tool backups. for (const entry of fs.readdirSync(path.join(webRoot, "tools"))) { if (/^tools\.bak\./.test(entry)) remove(path.join(webRoot, "tools", entry), "stale tools backup", items); } const totalMb = items.reduce((sum, item) => sum + item.mb, 0); for (const item of items.sort((a, b) => b.mb - a.mb).slice(0, 20)) { console.log(`${String(item.mb).padStart(6)} MB ${item.target} (${item.reason})`); } console.log(JSON.stringify({ apply, items: items.length, totalMb, freeBefore: null })); if (!apply) console.log("dry run only; pass --apply to delete");