| 24 | const maxDepth = 5; |
| 25 | |
| 26 | function clean(dir: string, depth: number) { |
| 27 | if (depth > maxDepth) return; |
| 28 | let entries: fs.Dirent[]; |
| 29 | try { |
| 30 | entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 31 | } catch { |
| 32 | return; |
| 33 | } |
| 34 | for (const entry of entries) { |
| 35 | const full = path.join(dir, entry.name); |
| 36 | if (entry.isDirectory()) { |
| 37 | if (nestedTargets.has(entry.name)) { |
| 38 | try { |
| 39 | fs.rmSync(full, { recursive: true, force: true }); |
| 40 | console.log("removed", full); |
| 41 | } catch (e: any) { |
| 42 | console.warn("skipped (in use?):", full, "-", e.message); |
| 43 | } |
| 44 | } else { |
| 45 | clean(full, depth + 1); |
| 46 | } |
| 47 | } else if (entry.isFile() && nestedGlobs.test(entry.name)) { |
| 48 | try { |
| 49 | fs.rmSync(full, { force: true }); |
| 50 | console.log("removed", full); |
| 51 | } catch (e: any) { |
| 52 | console.warn("skipped (in use?):", full, "-", e.message); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | for (const dir of searchRoots) { |
| 59 | clean(path.join(root, dir), 1); |