-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (30 loc) · 880 Bytes
/
index.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
33
34
35
36
37
38
const {lstatSync, readdirSync: fsReaddirSync} = require('fs');
const {join} = require('path');
const isFalse = () => false;
const isDirectory = (file) => lstatSync(file).isDirectory();
const readdirSync = (dir, onDir, onFile, ignoreDir = isFalse) => {
if (typeof dir !== 'string') {
return [];
}
let dirs;
try {
dirs = fsReaddirSync(dir).map(name => join(dir, name)).filter((f) => !ignoreDir(f));
} catch (error) {
dirs = [];
}
for (let i = 0, length = dirs.length; i < length; i++) {
const file = dirs[i];
if (isDirectory(file)) {
if (typeof onDir === 'function') {
onDir(file);
}
dirs = dirs.concat(...readdirSync(file, onDir, onFile, ignoreDir));
} else {
if (typeof onFile === 'function') {
onFile(file);
}
}
}
return dirs.filter(isDirectory);
};
module.exports = readdirSync;