输出当前的目录文件树
js
const fs = require('fs');
const path = require('path');
function printDirectoryTree(directory, depth = 3, indent = '') {
// 当深度为0时,停止遍历
if (depth < 0) return;
const entries = fs.readdirSync(directory, {withFileTypes: true});
for (const entry of entries) {
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
console.log(indent + '📂 ' + entry.name);
// 减小深度值并进行递归调用
printDirectoryTree(fullPath, depth - 1, indent + ' ');
} else {
console.log(indent + '📄 ' + entry.name);
}
}
}
const directoryPath = './src'; // 将这里改为你要输出的目录路径
printDirectoryTree(directoryPath, 1);