const fs = require('fs');
const path = require('path');
const util = require('util');
const traverse = function(dir, result = []) {
// list files in directory and loop through
fs.readdirSync(dir).forEach((file) => {
// builds full path of file
const fPath = path.resolve(dir, file);
// prepare stats obj
const fileStats = { file, path: fPath };
// is the file a directory ?
// if yes, traverse it also, if no just add it to the result
if (fs.statSync(fPath).isDirectory()) {
fileStats.type = 'dir';
fileStats.files = [];
result.push(fileStats);
return traverse(fPath, fileStats.files)
}
fileStats.type = 'file';
result.push(fileStats);
});
return result;
};
console.log(util.inspect(traverse(process.argv[2]), false, null));
1条答案
按热度按时间abithluo1#
这是一个递归的解决方案,你可以测试它,把它保存在一个文件中,运行
node yourfile.js /the/path/to/traverse
。输出如下所示: