node遞歸屬性目錄結構

要求,讀取結束後才能輸出全部文件ui

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);});
相關文章
相關標籤/搜索