All files / src/lib/helpers cache.ts

52.17% Statements 12/23
0% Branches 0/3
0% Functions 0/3
45% Lines 9/20

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 33 342x 2x 2x 2x 2x   2x   2x     2x         2x                                  
import Path from "path";
import { rimraf as rmdir } from "rimraf";
import glob from "glob";
import { promisify } from "util";
import { stat, unlink } from "fs-extra";
 
const globAsync = promisify(glob);
 
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 globAsync(Path.join(cacheRoot, "**/*"), {
    nodir: true,
  });
  const cutoffTime = Date.now() - age;
  for (const file of files) {
    try {
      const stats = await stat(file);
      Iif (stats.isFile() && stats.mtimeMs < cutoffTime) {
        await unlink(file);
      }
    } catch (err) {
      // Ignore errors (e.g. file not found)
    }
  }
};