|
1 | 1 | import fs from 'node:fs'
|
| 2 | +import fsp from 'node:fs/promises' |
2 | 3 | import path from 'node:path'
|
3 | 4 | import { performance } from 'node:perf_hooks'
|
4 | 5 | import _debug from 'debug'
|
@@ -879,7 +880,10 @@ export function getDepsCacheDir(config: ResolvedConfig, ssr: boolean): string {
|
879 | 880 |
|
880 | 881 | function getProcessingDepsCacheDir(config: ResolvedConfig, ssr: boolean) {
|
881 | 882 | return (
|
882 |
| - getDepsCacheDirPrefix(config) + getDepsCacheSuffix(config, ssr) + '_temp' |
| 883 | + getDepsCacheDirPrefix(config) + |
| 884 | + getDepsCacheSuffix(config, ssr) + |
| 885 | + '_temp_' + |
| 886 | + getHash(Date.now().toString()) |
883 | 887 | )
|
884 | 888 | }
|
885 | 889 |
|
@@ -1247,3 +1251,26 @@ export async function optimizedDepNeedsInterop(
|
1247 | 1251 | }
|
1248 | 1252 | return depInfo?.needsInterop
|
1249 | 1253 | }
|
| 1254 | + |
| 1255 | +const MAX_TEMP_DIR_AGE_MS = 24 * 60 * 60 * 1000 |
| 1256 | +export async function cleanupDepsCacheStaleDirs( |
| 1257 | + config: ResolvedConfig, |
| 1258 | +): Promise<void> { |
| 1259 | + try { |
| 1260 | + const cacheDir = path.resolve(config.cacheDir) |
| 1261 | + if (fs.existsSync(cacheDir)) { |
| 1262 | + const dirents = await fsp.readdir(cacheDir, { withFileTypes: true }) |
| 1263 | + for (const dirent of dirents) { |
| 1264 | + if (dirent.isDirectory() && dirent.name.includes('_temp_')) { |
| 1265 | + const tempDirPath = path.resolve(config.cacheDir, dirent.name) |
| 1266 | + const { mtime } = await fsp.stat(tempDirPath) |
| 1267 | + if (Date.now() - mtime.getTime() > MAX_TEMP_DIR_AGE_MS) { |
| 1268 | + await removeDir(tempDirPath) |
| 1269 | + } |
| 1270 | + } |
| 1271 | + } |
| 1272 | + } |
| 1273 | + } catch (err) { |
| 1274 | + config.logger.error(err) |
| 1275 | + } |
| 1276 | +} |
0 commit comments