Skip to content

Commit 69eae0b

Browse files
committed
keep a stack
1 parent 456e513 commit 69eae0b

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,16 @@ const options = {
4444
stat: true
4545
};
4646
47-
await walk('.', options);
47+
walk('.', options)
48+
.then(() => {
49+
console.log('total bytes in "."', ctx.total);
50+
});
4851
49-
console.log('total bytes in "."', ctx.total);
50-
51-
// executed in the await-walk package root it will print something like
52+
// executed in the action-walk package root it will print something like
5253
// total bytes in "." 14778
5354
```
5455

55-
see `test/basics.test.js` for another example.
56+
see `test/basics.test.js` or `bin/walk.js` for other examples.
5657

5758
### api
5859

@@ -63,9 +64,9 @@ options
6364
- `fileAction` - called for each file and, if `options.linkAction` is not set, each symbolic link.
6465
- `linkAction` - called for each symbolic link when `options.linkAction` is set.
6566
- `otherAction` - called when the entry is not a file, directory, or symbolic link.
66-
- `stat` - if `lstat` call `fs.lstat` on the entry and add it to the action context. if
67-
otherwise truthy use `fs.stat`.
68-
- `own` - add this to the action context. it is context for the action functions.
67+
- `stat` - if `'lstat'` call `fs.lstat` on the entry and add it to the action context's `stat`
68+
property. if otherwise truthy use `fs.stat`.
69+
- `own` - add this to the action context. it is your context for the action functions.
6970

7071
It's possible to call `walk()` with no options but probably not useful unless
7172
all you're wanting to do is seed the disk cache with directory entries. The
@@ -81,6 +82,7 @@ will be `test/basics.test.js`.
8182
{
8283
dirent, // the fs.Dirent object for the directory entry
8384
stat, // if `options.stat` the object returned by `fs.stat` or `fs.lstat`
85+
stack, // the stack of directories above the current dirent item.
8486
own // `options.own` if provided.
8587
}
8688
```

action-walk.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ async function walk (dir, options = {}) {
1313
} else if (options.stat) {
1414
stat = 'stat';
1515
}
16+
const stack = [];
1617

1718
if (options.fileAction) {
1819
fileAction = async (filepath, ctx) => options.fileAction(filepath, ctx);
@@ -31,13 +32,14 @@ async function walk (dir, options = {}) {
3132
// walk through a directory tree calling user functions for each entry.
3233
//
3334
async function walker (dir) {
35+
stack.push(path.basename(dir));
3436
for await (const dirent of await fsp.opendir(dir)) {
3537
let entry = path.join(dir, dirent.name);
3638
// path.join refuses to start a path with '.'
3739
if (dir === '.' || dir.startsWith('./')) {
3840
entry = './' + entry;
3941
}
40-
const ctx = {dirent};
42+
const ctx = {dirent, stack};
4143
if (options.own) {
4244
ctx.own = options.own;
4345
}
@@ -60,6 +62,7 @@ async function walk (dir, options = {}) {
6062
otherAction && await otherAction(entry, ctx);
6163
}
6264
}
65+
stack.pop();
6366
return undefined;
6467
}
6568

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "action-walk",
3-
"version": "2.1.1",
3+
"version": "2.2.0",
44
"description": "walk a directory tree performing actions",
55
"main": "action-walk.js",
66
"engines": {

0 commit comments

Comments
 (0)