max versions, remove old node_modules

This commit is contained in:
Chris Wanstrath 2026-02-13 20:34:20 -08:00
parent c10ebe3c98
commit 1b563106fe

View File

@ -5,6 +5,8 @@ import { cpSync, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync,
import { dirname, join } from 'path' import { dirname, join } from 'path'
import { Hype } from '@because/hype' import { Hype } from '@because/hype'
const MAX_VERSIONS = 5
interface FileChangeEvent { interface FileChangeEvent {
hash?: string hash?: string
path: string path: string
@ -294,7 +296,7 @@ router.post('/apps/:app/activate', async c => {
// Atomic rename over old symlink/directory // Atomic rename over old symlink/directory
renameSync(tempLink, currentLink) renameSync(tempLink, currentLink)
// Clean up old versions (keep 5 most recent) // Clean up old versions
try { try {
const entries = readdirSync(appDir, { withFileTypes: true }) const entries = readdirSync(appDir, { withFileTypes: true })
@ -305,13 +307,24 @@ router.post('/apps/:app/activate', async c => {
.sort() .sort()
.reverse() // Newest first .reverse() // Newest first
// Keep 5 most recent, delete the rest // Keep most recent, delete the rest
const toDelete = versionDirs.slice(5) const toDelete = versionDirs.slice(MAX_VERSIONS)
for (const dir of toDelete) { for (const dir of toDelete) {
const dirPath = join(appDir, dir) const dirPath = join(appDir, dir)
rmSync(dirPath, { recursive: true, force: true }) rmSync(dirPath, { recursive: true, force: true })
console.log(`Cleaned up old version: ${dir}`) console.log(`Cleaned up old version: ${dir}`)
} }
// Delete node_modules from old kept versions (not the active one)
const toKeep = versionDirs.slice(0, MAX_VERSIONS)
for (const dir of toKeep) {
if (dir === version) continue
const nm = join(appDir, dir, 'node_modules')
if (existsSync(nm)) {
rmSync(nm, { recursive: true, force: true })
console.log(`Removed node_modules from old version: ${dir}`)
}
}
} catch (e) { } catch (e) {
// Log but don't fail activation if cleanup fails // Log but don't fail activation if cleanup fails
console.error(`Failed to clean up old versions: ${e}`) console.error(`Failed to clean up old versions: ${e}`)