clean-turbopack-cache.mjs 688 B

123456789101112131415161718192021
  1. import { existsSync, readdirSync, rmSync, statSync } from 'fs'
  2. import { join } from 'path'
  3. // This script cleans up the Turbopack cache by removing files that haven't been modified in the last 3 days. This is to
  4. // prevent the cache from growing indefinitely and consuming too much RAM.
  5. const dir = '.next/dev/cache/turbopack'
  6. const cutoff = Date.now() - 3 * 24 * 60 * 60 * 1000 // 3 days in milliseconds
  7. function clean(d) {
  8. if (!existsSync(d)) return
  9. for (const entry of readdirSync(d, { withFileTypes: true })) {
  10. const p = join(d, entry.name)
  11. if (entry.isDirectory()) {
  12. clean(p)
  13. } else if (statSync(p).mtimeMs < cutoff) {
  14. rmSync(p)
  15. }
  16. }
  17. }
  18. clean(dir)