-
Notifications
You must be signed in to change notification settings - Fork 0
/
listFolders.js
39 lines (32 loc) · 1.13 KB
/
listFolders.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
import { readdirSync, statSync } from 'fs';
import { join } from 'path';
// 获取指定目录中的文件夹列表,排除 node_modules
function listFoldersInDirectory(directory) {
return readdirSync(directory).filter(file => {
const fullPath = join(directory, file);
return statSync(fullPath).isDirectory() && ['.git','node_modules'].includes(file)===false;
});
}
// 递归列出当前目录及其子目录中的文件夹
function listFoldersRecursive(directory, level = 0) {
const folders = listFoldersInDirectory(directory);
let markdown = '';
folders.forEach(folder => {
const folderPath = join(directory, folder);
markdown += `${' '.repeat(level)}- ${folder}\n`;
markdown += listFoldersRecursive(folderPath, level + 1);
});
return markdown;
}
// 主函数
function main() {
const directory = process.cwd(); // 当前工作目录
const markdown = listFoldersRecursive(directory);
if (markdown) {
console.log("Markdown format:\n");
console.log(markdown);
} else {
console.log("No folders found in the directory.");
}
}
main();