-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
listFiles.js
31 lines (30 loc) · 836 Bytes
/
listFiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @description List files of provided path
*/
const fs = require('fs');
const path = require('path');
/**
* Lists files in the supplied directory path
* @param {String} dirPath The path to the dir
* @return {Array} An array that contains all the file paths
*/
module.exports = function listFiles(dirPath) {
try {
// 指定目录下所有文件名称
const lsDir = fs.readdirSync(dirPath);
const filesArr = [];
for (const fileName of lsDir) {
const pathName = path.join(dirPath, fileName);
if (fs.statSync(pathName).isDirectory()) {
// 三点运算符
filesArr.push(...listFiles(pathName));
} else {
filesArr.push(pathName);
}
}
return filesArr;
} catch (e) {
console.warn("Not Found dirPath Files:" + dirPath);
return null;
}
};