Skip to content

Commit 2d832ae

Browse files
committed
introduce fs_walk
1 parent d743110 commit 2d832ae

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/fs_walk.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const fs_lstat = require('./fs_lstat');
2+
const fs_path_join = require('./fs_path_join');
3+
const fs_readdir = require('./fs_readdir');
4+
const ignore = require('./ignore');
5+
6+
async function fs_walk({path = '.', user_friendly_status = ignore, error_handler = error_handler, signal})
7+
{
8+
const out = [];
9+
const queue = [path];
10+
while (queue.length) {
11+
if (signal) {
12+
signal.throwIfAborted();
13+
}
14+
try {
15+
const current_path = queue.pop();
16+
user_friendly_status(`Reading ${current_path}`);
17+
const lstat = await fs_lstat(current_path);
18+
lstat.path = current_path;
19+
out.push(lstat);
20+
if (lstat.isDirectory()) {
21+
if (current_path === '/dev' || current_path === '/proc') {
22+
continue;
23+
}
24+
const basenames = await fs_readdir(current_path);
25+
basenames.forEach(v => queue.push(fs_path_join(current_path, v)));
26+
}
27+
}
28+
catch (error) {
29+
error_handler(error);
30+
}
31+
}
32+
return out;
33+
}
34+
35+
function error_handler(error)
36+
{
37+
console.log(`⚠️ ${error.message}`);
38+
}
39+
40+
module.exports = fs_walk;

0 commit comments

Comments
 (0)