Repository navigation
Expand file tree
/
Copy pathadvanced.js
More file actions
59 lines (48 loc) · 1.64 KB
/
Copy pathadvanced.js
File metadata and controls
59 lines (48 loc) · 1.64 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
import { removeItem } from "remove-item";
// Mixed cleanup: files + directories in one call
const results = await removeItem(
["dist", "coverage", ".cache", "temp.log"],
{ recursive: true, force: true }
);
// Inspect results
for (const result of results) {
if (result.success) {
console.log(`✓ Removed: ${result.path}`);
} else {
console.error(`✗ Failed: ${result.path} — ${result.error?.message}`);
}
}
// Safe cleanup workflow: dry-run => confirm => delete
async function safeCleanup(paths) {
console.log("Previewing deletion...");
await removeItem(paths, { dryRun: true, recursive: true, verbose: true });
console.log("Performing actual deletion...");
const outcomes = await removeItem(paths, { recursive: true, force: true });
const failed = outcomes.filter((r) => !r.success);
if (failed.length > 0) {
console.error(`${failed.length} item(s) could not be removed.`);
process.exit(1);
}
console.log("Cleanup complete.");
}
await safeCleanup(["build", ".temp"]);
// Conditional cleanup based on file metadata
import { stat, readdir } from "node:fs/promises";
import { join } from "node:path";
async function cleanOldLogs(dir, maxDays = 7) {
const now = Date.now();
const cutoff = maxDays * 24 * 60 * 60 * 1000;
const entries = await readdir(dir);
const old = [];
for (const entry of entries) {
if (!entry.endsWith(".log")) continue;
const p = join(dir, entry);
const s = await stat(p);
if (now - s.mtimeMs > cutoff) old.push(p);
}
if (old.length > 0) {
console.log(`Removing ${old.length} old log(s)...`);
await removeItem(old, { force: true });
}
}
await cleanOldLogs("./logs");