|
| 1 | +// # Move all the existing cache into another folder, so we only preserve the cache for the current queries. |
| 2 | +// mkdir -p ${COMBINED_CACHE_DIR} |
| 3 | +// rm -f **/.cache/{lock,size} # -f to avoid errors if the cache is empty. |
| 4 | +// # copy the contents of the .cache folders into the combined cache folder. |
| 5 | +// cp -r **/.cache/* ${COMBINED_CACHE_DIR}/ || : # ignore missing files |
| 6 | +// # clean up the .cache folders |
| 7 | +// rm -rf **/.cache/* |
| 8 | + |
| 9 | +const fs = require("fs"); |
| 10 | +const path = require("path"); |
| 11 | + |
| 12 | +// the first argv is the cache folder to create. |
| 13 | +const COMBINED_CACHE_DIR = process.argv[2]; |
| 14 | + |
| 15 | +function* walkCaches(dir) { |
| 16 | + const files = fs.readdirSync(dir); |
| 17 | + for (const file of files) { |
| 18 | + const filePath = path.join(dir, file); |
| 19 | + if (!fs.existsSync(filePath)) { |
| 20 | + continue; |
| 21 | + } |
| 22 | + const stat = fs.statSync(filePath); |
| 23 | + if (stat.isDirectory()) { |
| 24 | + yield* walkCaches(filePath); |
| 25 | + if (file === ".cache") { |
| 26 | + yield filePath; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +async function copyDir(src, dest) { |
| 33 | + for await (const file of await fs.promises.readdir(src)) { |
| 34 | + const srcPath = path.join(src, file); |
| 35 | + const destPath = path.join(dest, file); |
| 36 | + const stat = await fs.promises.stat(srcPath); |
| 37 | + if (stat.isDirectory()) { |
| 38 | + if (!fs.existsSync(destPath)) { |
| 39 | + fs.mkdirSync(destPath); |
| 40 | + } |
| 41 | + await copyDir(srcPath, destPath); |
| 42 | + } else { |
| 43 | + await fs.promises.copyFile(srcPath, destPath); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function main() { |
| 49 | + const cacheDirs = [...walkCaches(".")]; |
| 50 | + |
| 51 | + for (const dir of cacheDirs) { |
| 52 | + console.log(`Found .cache dir at ${dir}`); |
| 53 | + } |
| 54 | + |
| 55 | + // mkdir -p ${COMBINED_CACHE_DIR} |
| 56 | + fs.mkdirSync(COMBINED_CACHE_DIR, { recursive: true }); |
| 57 | + |
| 58 | + // rm -f **/.cache/{lock,size} # -f to avoid errors if the cache is empty. |
| 59 | + await Promise.all( |
| 60 | + cacheDirs.map((cacheDir) => |
| 61 | + (async function () { |
| 62 | + await fs.promises.rm(path.join(cacheDir, "lock"), { force: true }); |
| 63 | + await fs.promises.rm(path.join(cacheDir, "size"), { force: true }); |
| 64 | + })() |
| 65 | + ) |
| 66 | + ); |
| 67 | + |
| 68 | + // # copy the contents of the .cache folders into the combined cache folder. |
| 69 | + // cp -r **/.cache/* ${COMBINED_CACHE_DIR}/ || : # ignore missing files |
| 70 | + await Promise.all( |
| 71 | + cacheDirs.map((cacheDir) => copyDir(cacheDir, COMBINED_CACHE_DIR)) |
| 72 | + ); |
| 73 | + |
| 74 | + // # clean up the .cache folders |
| 75 | + // rm -rf **/.cache/* |
| 76 | + await Promise.all( |
| 77 | + cacheDirs.map((cacheDir) => fs.promises.rm(cacheDir, { recursive: true })) |
| 78 | + ); |
| 79 | +} |
| 80 | +main(); |
0 commit comments