diff --git a/README.md b/README.md index ad9e49c..20165d5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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. } ``` diff --git a/action-walk.js b/action-walk.js index 07a901c..0473f85 100644 --- a/action-walk.js +++ b/action-walk.js @@ -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); @@ -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; } @@ -60,6 +62,7 @@ async function walk (dir, options = {}) { otherAction && await otherAction(entry, ctx); } } + stack.pop(); return undefined; } diff --git a/package.json b/package.json index 2184e80..df82172 100644 --- a/package.json +++ b/package.json @@ -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": {