|
| 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, { withFileTypes: true }); |
| 17 | + for (const file of files) { |
| 18 | + if (file.isDirectory()) { |
| 19 | + const filePath = path.join(dir, file.name); |
| 20 | + yield* walkCaches(filePath); |
| 21 | + if (file.name === ".cache") { |
| 22 | + yield filePath; |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +async function copyDir(src, dest) { |
| 29 | + for await (const file of await fs.promises.readdir(src, { withFileTypes: true })) { |
| 30 | + const srcPath = path.join(src, file.name); |
| 31 | + const destPath = path.join(dest, file.name); |
| 32 | + if (file.isDirectory()) { |
| 33 | + if (!fs.existsSync(destPath)) { |
| 34 | + fs.mkdirSync(destPath); |
| 35 | + } |
| 36 | + await copyDir(srcPath, destPath); |
| 37 | + } else { |
| 38 | + await fs.promises.copyFile(srcPath, destPath); |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +async function main() { |
| 44 | + const cacheDirs = [...walkCaches(".")]; |
| 45 | + |
| 46 | + for (const dir of cacheDirs) { |
| 47 | + console.log(`Found .cache dir at ${dir}`); |
| 48 | + } |
| 49 | + |
| 50 | + // mkdir -p ${COMBINED_CACHE_DIR} |
| 51 | + fs.mkdirSync(COMBINED_CACHE_DIR, { recursive: true }); |
| 52 | + |
| 53 | + // rm -f **/.cache/{lock,size} # -f to avoid errors if the cache is empty. |
| 54 | + await Promise.all( |
| 55 | + cacheDirs.map((cacheDir) => |
| 56 | + (async function () { |
| 57 | + await fs.promises.rm(path.join(cacheDir, "lock"), { force: true }); |
| 58 | + await fs.promises.rm(path.join(cacheDir, "size"), { force: true }); |
| 59 | + })() |
| 60 | + ) |
| 61 | + ); |
| 62 | + |
| 63 | + // # copy the contents of the .cache folders into the combined cache folder. |
| 64 | + // cp -r **/.cache/* ${COMBINED_CACHE_DIR}/ || : # ignore missing files |
| 65 | + await Promise.all( |
| 66 | + cacheDirs.map((cacheDir) => copyDir(cacheDir, COMBINED_CACHE_DIR)) |
| 67 | + ); |
| 68 | + |
| 69 | + // # clean up the .cache folders |
| 70 | + // rm -rf **/.cache/* |
| 71 | + await Promise.all( |
| 72 | + cacheDirs.map((cacheDir) => fs.promises.rm(cacheDir, { recursive: true })) |
| 73 | + ); |
| 74 | +} |
| 75 | +main(); |
0 commit comments