代码
const fs = require('fs')
function filesFind(cfg){
let arrFiles = []
;(function find(fPath, extract, ignoreFile, isRecursion){
let arr = fs.readdirSync(fPath)
arr.forEach((value, index, arr)=>{
if(fs.statSync(`${fPath}/${value}`).isDirectory()){
if(isRecursion) find(`${fPath}/${value}`, extract, ignoreFile, isRecursion)
else arrFiles.push(`${fPath}/${value}`)
}
else {
if(extract.slice(-1)[0]){
if(!extract.some((val) => value.endsWith(val))) {
return
}
}
if(! ignoreFile.some((val)=> value.endsWith(val))){
arrFiles.push(`${fPath}/${value}`)
}
}
})
})(cfg.path, cfg.extract, cfg.ignoreFile, cfg.isRecursion)
return arrFiles
}
let config = {
path: __dirname,
extract: ["html"],
ignoreFile: [],
isRecursion: true
}
console.log(filesFind(config ))
结果演示
- 可以自己修改配置进行测试