-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
79 lines (65 loc) · 2.02 KB
/
utils.js
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
74
75
76
77
78
79
const { execSync } = require("child_process");
const fs = require("fs").promises;
const path = require("path");
function getCurrentDir() {
try {
const pwd = execSync("pwd").toString().trim();
return pwd;
} catch (error) {
console.error("Error retrieving current directory:", error);
process.exit(1);
}
}
async function readDirectoryString(dirPath) {
const dirStructure = await readDirectoryRecursive(dirPath);
function formatDirectoryStructure(structure, indent = "") {
let result = "";
for (let i = 0; i < structure.length; i++) {
const item = structure[i];
const isLast = i === structure.length - 1;
const prefix = isLast ? "└── " : "├── ";
const nextIndent = indent + (isLast ? " " : "│ ");
if (typeof item === "string") {
result += `${indent}${prefix}${item}\n`;
} else if (typeof item === "object") {
const folderName = Object.keys(item)[0];
result += `${indent}${prefix}${folderName}\n`;
result += formatDirectoryStructure(item[folderName], nextIndent);
}
}
return result;
}
return formatDirectoryStructure(dirStructure);
}
async function readDirectoryRecursive(
dirPath,
ignorePaths = ["node_modules", "venv", ".git", ".next"],
) {
const result = [];
try {
const items = await fs.readdir(dirPath, { withFileTypes: true });
for (const item of items) {
const itemPath = path.join(dirPath, item.name);
const relativePath = path.relative(dirPath, itemPath);
if (ignorePaths.some((ignorePath) => relativePath.includes(ignorePath))) {
continue;
}
if (item.isDirectory()) {
const subDirStructure = await readDirectoryRecursive(
itemPath,
ignorePaths,
);
result.push({ [item.name]: subDirStructure });
} else {
result.push(item.name);
}
}
} catch (error) {
console.error("Error reading directory:", error);
}
return result;
}
module.exports = {
getCurrentDir,
readDirectoryString,
};