-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdiffFolders.mjs
More file actions
73 lines (58 loc) · 1.77 KB
/
diffFolders.mjs
File metadata and controls
73 lines (58 loc) · 1.77 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
61
62
63
64
65
66
67
68
69
70
71
72
73
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
// __dirname for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
function getStructure(dir, base = dir) {
const structure = new Set();
function walk(current) {
const entries = fs.readdirSync(current, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(current, entry.name);
const relativePath = path.relative(base, fullPath);
structure.add(relativePath);
if (entry.isDirectory()) {
walk(fullPath);
}
}
}
walk(dir);
return structure;
}
function compareFolders(folderA, folderB) {
const structureA = getStructure(folderA);
const structureB = getStructure(folderB);
const differences = [];
for (const entry of structureA) {
if (!structureB.has(entry)) {
differences.push({ type: "missing_in_b", path: entry });
}
}
for (const entry of structureB) {
if (!structureA.has(entry)) {
differences.push({ type: "missing_in_a", path: entry });
}
}
return differences;
}
// --- CLI Usage ---
const folderA = process.argv[2];
const folderB = process.argv[3];
if (!folderA || !folderB) {
console.error("Usage: node compareStructure.mjs <folderA> <folderB>");
process.exit(1);
}
const diffs = compareFolders(folderA, folderB);
const nameA = path.basename(path.resolve(folderA));
const nameB = path.basename(path.resolve(folderB));
if (diffs.length === 0) {
console.log("✅ Folder structures are identical");
} else {
console.log("❌ Differences found:");
for (const diff of diffs) {
const label =
diff.type === "missing_in_a" ? `Only in ${nameB}` : `Only in ${nameA}`;
console.log(`${label}: ${diff.path}`);
}
}