直接上代码
const fs = require('fs');
const path = require('path');
const loadfile = async (filePath, data, suffix) => {
if (undefined === data) {
data = [];
}
// 如果路径为目录,则遍历该目录
if (fs.statSync(filePath).isDirectory()) {
fs.readdirSync(filePath).forEach(
file => loadfile(path.join(filePath, file), data, suffix)
);
}
else {
if (filePath.endsWith(suffix)) {
data.push(filePath);
}
}
return data;
}