遍历目录模块 fsext.js
var fs = require('fs');
var path = require('path');
// 同步遍历(因为有递归,所以需要单独写function)
// 参照http://nqdeng.github.io/7-days-nodejs/#3.3
let getFilesRecursion = function(dir, callback) {
fs.readdirSync(dir).forEach(function (file) {
var pathname = path.join(dir, file);
if (fs.statSync(pathname).isDirectory()) {
getFilesRecursion(pathname, callback);
} else {
callback(pathname);
}
})
}
let fsext = {
getAllFile : getFilesRecursion,
// 扩展名过滤
filterFile : function(fileNameList, fileName, extName) {
if (fileName.endsWith(extName)) {
fileNameList.push(fileName);
// console.log(fileName);
}
}
}
module.exports = fsext;