-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathclean.ts
More file actions
60 lines (55 loc) · 1.67 KB
/
Copy pathclean.ts
File metadata and controls
60 lines (55 loc) · 1.67 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env bun
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const rootTargets = ["node_modules", ".turbo", ".astro", "dist"];
for (const name of rootTargets) {
const p = path.join(root, name);
if (fs.existsSync(p)) {
try {
fs.rmSync(p, { recursive: true, force: true });
console.log("removed", p);
} catch (e: any) {
console.warn("skipped (in use?):", p, "-", e.message);
}
}
}
const nestedTargets = new Set(["node_modules", "dist", ".turbo", ".output", ".astro"]);
const nestedGlobs = /\.tsbuildinfo$/;
const searchRoots = ["apps", "packages"];
const maxDepth = 5;
function clean(dir: string, depth: number) {
if (depth > maxDepth) return;
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (nestedTargets.has(entry.name)) {
try {
fs.rmSync(full, { recursive: true, force: true });
console.log("removed", full);
} catch (e: any) {
console.warn("skipped (in use?):", full, "-", e.message);
}
} else {
clean(full, depth + 1);
}
} else if (entry.isFile() && nestedGlobs.test(entry.name)) {
try {
fs.rmSync(full, { force: true });
console.log("removed", full);
} catch (e: any) {
console.warn("skipped (in use?):", full, "-", e.message);
}
}
}
}
for (const dir of searchRoots) {
clean(path.join(root, dir), 1);
}