All files / src/lib/helpers cache.ts

47.61% Statements 10/21
0% Branches 0/4
0% Functions 0/3
38.88% Lines 7/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x 1x 1x 1x   1x     1x         1x                                      
import Path from "path";
import { rimraf as rmdir } from "rimraf";
import { glob } from "lib/helpers/glob";
import { stat, unlink } from "fs-extra";
 
export const getCacheRoot = (tmpPath: string) =>
  Path.join(tmpPath, "_gbscache");
 
export const clearAppCache = async (tmpPath: string) => {
  const cacheRoot = getCacheRoot(tmpPath);
  await rmdir(cacheRoot);
};
 
export const clearAppCacheOlderThan = async (tmpPath: string, age: number) => {
  const cacheRoot = getCacheRoot(tmpPath);
  const files = await glob("**/*", {
    cwd: cacheRoot,
    absolute: true,
    nodir: true,
  });
  const cutoffTime = Date.now() - age;
  for (const file of files) {
    try {
      const stats = await stat(file);
      if (stats.isFile() && stats.mtimeMs < cutoffTime) {
        await unlink(file);
      }
    } catch {
      // Ignore errors (e.g. file not found)
    }
  }
};