-
Notifications
You must be signed in to change notification settings - Fork 755
Open
Description
我照着你的写,但只能编译同一个目录下的一个文件,然后我在想可能没有循环,我认为应该类似这样的:
`function travel(dir,callback,finish){
//异步读取目录
fs.readdir(dir,function(err,files){
//将0传给next,开始递归
for(i=0;i<files.length;++i) {
(function next(i){
//如果当前未完成遍历
if (i<files.length){
//获取到路径文件名
var pathname = path.join(dir, files[i]);
//异步获取此路径文件名的文件状态
fs.stat(pathname,function(err,stats){
//如果此文件是目录
if (stats.isDirectory()){
//再次递归此目录
travel(pathname, callback, function () {
//如果此目录递归完了,继续检查上一层的下一个文件
next(i + 1);
});
}
//此文件是非目录
else {
//执行上层所需的callback函数,完成后继续递归此层的下一个文件
callback(pathname,function(){
next(i + 1);
})
}
})
}
//如果所有文件已经完成遍历
else{
//如果有完成回调函数,执行完成回调函数
finish && finish();
}
}(i))
}
})
}
travel("./",function(pathname){
console.log(pathname);
})`
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels