-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (42 loc) · 1.1 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const path = require('path');
const {
lstatSync,
readdirSync,
} = require('fs');
const currify = require('currify/legacy');
const isString = (a) => typeof a === 'string';
const getDirEnt = currify(_getDirEnt);
module.exports = (dir) => {
const names = readdirSync(dir, {
withFileTypes: true
});
if (!names.length)
return [];
if (!isString(names[0]))
return names;
return names.map(getDirEnt(dir));
};
function _getDirEnt(dir, name) {
const fullPath = path.join(dir, name);
const stat = lstatSync(fullPath);
const {
isBlockDevice,
isCharacterDevice,
isDirectory,
isFIFO,
isFile,
isSocket,
isSymbolicLink,
} = stat;
return {
name,
isBlockDevice: isBlockDevice.bind(stat),
isCharacterDevice: isCharacterDevice.bind(stat),
isDirectory: isDirectory.bind(stat),
isFIFO: isFIFO.bind(stat),
isFile: isFile.bind(stat),
isSocket: isSocket.bind(stat),
isSymbolicLink: isSymbolicLink.bind(stat),
};
}