UNPKG

1.37 kBJavaScriptView Raw
1const { lstat, lstatSync, readdir, readdirSync, Dirent } = require("fs");
2const { sep } = require("path");
3
4/* istanbul ignore next */
5if (!Dirent) {
6 module.exports.readdir = function(dir, _, callback) {
7 readdir(dir, (err, files) => {
8 if (err) return process.nextTick(callback, err, null);
9 if (!files.length) return process.nextTick(callback, null, []);
10
11 let dirents = [];
12
13 for (let i = 0; i < files.length; ++i) {
14 let name = files[i];
15 let path = `${dir}${sep}${name}`;
16 lstat(path, (err, stat) => {
17 if (err) return process.nextTick(callback, err, null);
18 dirents[dirents.length] = getDirent(name, stat);
19 if (dirents.length === files.length) {
20 process.nextTick(callback, null, dirents);
21 }
22 });
23 }
24 });
25 };
26
27 module.exports.readdirSync = function(dir) {
28 const files = readdirSync(dir);
29 let dirents = [];
30 for (let i = 0; i < files.length; ++i) {
31 let name = files[i];
32 let path = `${dir}${sep}${name}`;
33 const stat = lstatSync(path);
34 dirents[dirents.length] = getDirent(name, stat);
35 }
36 return dirents;
37 };
38
39 function getDirent(name, stat) {
40 return {
41 name,
42 isFile: () => stat.isFile(),
43 isDirectory: () => stat.isDirectory(),
44 };
45 }
46} else {
47 module.exports = { readdirSync, readdir };
48}