(n = 5)
| 9 | const debug = Debug('cleanupCache') |
| 10 | |
| 11 | export async function cleanupCache(n = 5): Promise<void> { |
| 12 | try { |
| 13 | const rootCacheDir = await getRootCacheDir() |
| 14 | if (!rootCacheDir) { |
| 15 | debug('no rootCacheDir found') |
| 16 | return |
| 17 | } |
| 18 | const channel = 'master' |
| 19 | const cacheDir = path.join(rootCacheDir, channel) |
| 20 | const dirs = await fs.promises.readdir(cacheDir) |
| 21 | const dirsWithMeta = await Promise.all( |
| 22 | dirs.map(async (dirName) => { |
| 23 | const dir = path.join(cacheDir, dirName) |
| 24 | const statResult = await fs.promises.stat(dir) |
| 25 | |
| 26 | return { |
| 27 | dir, |
| 28 | created: statResult.birthtime, |
| 29 | } |
| 30 | }), |
| 31 | ) |
| 32 | dirsWithMeta.sort((a, b) => (a.created < b.created ? 1 : -1)) |
| 33 | const dirsToRemove = dirsWithMeta.slice(n) |
| 34 | await pMap(dirsToRemove, (dir) => fs.promises.rm(dir.dir, { force: true, recursive: true }), { concurrency: 20 }) |
| 35 | } catch (e) { |
| 36 | // fail silently |
| 37 | } |
| 38 | } |
no test coverage detected