-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetallfiles.js
32 lines (27 loc) · 1.01 KB
/
getallfiles.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
32
const fs = require('fs');
const path = require('path');
const getAllFiles = function (dirPath, extensions = ['.js'], arrayOfFiles = []) {
files = fs.readdirSync(dirPath);
files.forEach(function (file) {
if (fs.statSync(path.join(dirPath, '/', file)).isDirectory()) {
arrayOfFiles = getAllFiles(path.join(dirPath, '/', file), extensions, arrayOfFiles);
} else {
if (path.basename(file) === path.basename(__filename)) return;
if (extensions.includes(path.extname(file).toLowerCase())) {
arrayOfFiles.push({
path: path.join(dirPath, '/', file),
time: fs.statSync(dirPath + '/' + file).mtime.getTime(),
});
}
}
});
return arrayOfFiles;
};
const getLatestFile = (dirname, extensions) => {
let arrayOfFiles = getAllFiles(dirname, extensions);
arrayOfFiles.sort((a, b) => a.time - b.time);
return arrayOfFiles.slice(-1)[0];
};
// console.log(getAllFiles(__dirname));
console.log(getLatestFile(__dirname));
module.exports = { getAllFiles, getLatestFile };