递归读取文件夹,获取文件信息
const fs = require('fs');
const path = require('path');
const htmlList = [];
const cssList = [];
const jsList = [];
function getFiles(dir) {
const folders = fs.readdirSync(dir);
folders.forEach((folderName) => {
const folderOrFilePath = path.join(dir, folderName);
const stats = fs.statSync(folderOrFilePath);
if (stats.isDirectory()) {
getFiles(folderOrFilePath);
} else {
if (/[\w]+\.html$/.test(folderOrFilePath)) {
htmlList.push(folderOrFilePath);
}
else if (/[\w]+\.(css|map)$/.test(folderOrFilePath)) {
cssList.push(folderOrFilePath);
} else {
jsList.push(folderOrFilePath);
}
}
});
}
getFiles(__dirname);
console.log(`html: ${htmlList.length}`, `css: ${cssList.length}`, `js: ${jsList.length}`);