node递归属性目录结构
要求,读取结束后才能输出所有文件
var fs = require('fs');
var path = require('path');
var list = [];
var count = 0;
function readDir(_path, callback) {
var toExec = function (_path) {
count++;
fs.readdir(_path, function (err, files) {
if (err) {
console.log(err);
return;
}
files.forEach(function (file, i) {
var stat = fs.lstatSync(path.join(_path, file));
if (stat.isFile()) {
list.push(path.join(_path, file));
} else if (stat.isDirectory()) {
toExec(path.join(_path, file));
}
if ((i + 1) === files.length) {
count--;
}
if(count === 0){
callback(list);
}
});
});
};
toExec(_path);
};
readDir(path.join(process.cwd(), 'go'), function (_list) {
console.log(_list);
});