Skip to content

Commit

Permalink
keep a stack
Browse files Browse the repository at this point in the history
  • Loading branch information
bmacnaughton committed Oct 18, 2020
1 parent 456e513 commit 69eae0b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ const options = {
stat: true
};
await walk('.', options);
walk('.', options)
.then(() => {
console.log('total bytes in "."', ctx.total);
});
console.log('total bytes in "."', ctx.total);
// executed in the await-walk package root it will print something like
// executed in the action-walk package root it will print something like
// total bytes in "." 14778
```

see `test/basics.test.js` for another example.
see `test/basics.test.js` or `bin/walk.js` for other examples.

### api

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

It's possible to call `walk()` with no options but probably not useful unless
all you're wanting to do is seed the disk cache with directory entries. The
Expand All @@ -81,6 +82,7 @@ will be `test/basics.test.js`.
{
dirent, // the fs.Dirent object for the directory entry
stat, // if `options.stat` the object returned by `fs.stat` or `fs.lstat`
stack, // the stack of directories above the current dirent item.
own // `options.own` if provided.
}
```
Expand Down
5 changes: 4 additions & 1 deletion action-walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function walk (dir, options = {}) {
} else if (options.stat) {
stat = 'stat';
}
const stack = [];

if (options.fileAction) {
fileAction = async (filepath, ctx) => options.fileAction(filepath, ctx);
Expand All @@ -31,13 +32,14 @@ async function walk (dir, options = {}) {
// walk through a directory tree calling user functions for each entry.
//
async function walker (dir) {
stack.push(path.basename(dir));
for await (const dirent of await fsp.opendir(dir)) {
let entry = path.join(dir, dirent.name);
// path.join refuses to start a path with '.'
if (dir === '.' || dir.startsWith('./')) {
entry = './' + entry;
}
const ctx = {dirent};
const ctx = {dirent, stack};
if (options.own) {
ctx.own = options.own;
}
Expand All @@ -60,6 +62,7 @@ async function walk (dir, options = {}) {
otherAction && await otherAction(entry, ctx);
}
}
stack.pop();
return undefined;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "action-walk",
"version": "2.1.1",
"version": "2.2.0",
"description": "walk a directory tree performing actions",
"main": "action-walk.js",
"engines": {
Expand Down

0 comments on commit 69eae0b

Please sign in to comment.